在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
)
使用