在php中可以使用subst()的方法进行字符串截取
echo substr("hello wo", 5, 3);
在界面就会显示为 wo
使用explode()进行截取
$sti = "ww.ba.com"; print_r(explode(".", $sti));
页面显示Array ( [0] => ww [1] => ba [2] => com )
explode() 函数是使用一个字符串分割另一个字符串,并返回由字符串组成的数组。
explode函数有三个参数
$delimiter, $string, $limit
$delimiter是必需。规定在哪里分割字符串。并且不能为空字符串
string是要进行分割的字符串,也是必须的
limit是规定所返回的数组元素的数目,是可选的
实例如下
<?php $str = 'one,two,three,four'; // 返回包含一个元素的数组 print_r(explode(',',$str,0)); print "<br>"; // 数组元素为 2 print_r(explode(',',$str,2)); print "<br>"; // 删除最后一个数组元素 print_r(explode(',',$str,-1)); ?>
Array
(
[0] => one,two,three,four
)
Array
(
[0] => one
[1] => two,three,four
)
Array
(
[0] => one
[1] => two
[2] => three
)
使用