可通过explode的灵活用法,获取字符串的指定内容
explode — 使用一个字符串分割另一个字符串
用法
array explode ( string separator, string string [, int limit] )
例子1
在本例中,我们将把字符串分割为数组:
<?php $str = "Hello world. It's a beautiful day." ; print_r (explode( " " ,$str)); $str2 = explode( " " ,$str); ?> |
输出:
1、 Array ( [0] => Hello [1] => world. [2] => It's [3] => a [4] => beautiful [5] => day. ) 2、 echo $str2[1]; //world . echo $str2[2]; //It 's |
例子2
$str = "who am i , i am php" ; list($a,$b,$c,$d,$e,$f,$g) = explode( " " , $str); echo $c; //i echo $f; //am |
例子3
limit 参数示例
<?php $str = 'one|two|three|four' ; // 正数的 limit print_r(explode( '|' , $str, 2)); // 负数的 limit print_r(explode( '|' , $str, -1)); ?> |
以上示例将输出:
Array ( [0] => one [1] => two|three|four ) Array ( [0] => one [1] => two [2] => three ) |