示例 #1 preg_split()示例:获取搜索字符串的部分
<?php
//使用逗号或空格(包含" ", \r, \t, \n, \f)分隔短语
$keywords = preg_split("/[\s,]+/", "hypertext language, programming");
print_r($keywords);
?>
以上例程会输出:
Array ( [0] => hypertext [1] => language [2] => programming )
示例 #2 将一个字符串分隔为组成它的字符
<?php
$str = 'string';
$chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
print_r($chars);
?>
以上例程会输出:
Array ( [0] => s [1] => t [2] => r [3] => i [4] => n [5] => g )
示例 #3 分隔一个字符串并获取每部分的偏移量
<?php
$str = 'hypertext language programming';
$chars = preg_split('/ /', $str, -1, PREG_SPLIT_OFFSET_CAPTURE);
print_r($chars);
?>
以上例程会输出:
Array ( [0] => Array ( [0] => hypertext [1] => 0 ) [1] => Array ( [0] => language [1] => 10 ) [2] => Array ( [0] => programming [1] => 19 ) )
http://www.lvesu.com/blog/php/function.preg-split.php
最新评论: