preg_split -- 正規表達式切割字串
說明:
array preg_split ( string pattern, string subject [, int limit [, int flags]])
注: 在4版beta3中 加入選項
尋找相符字串進行字串切割.如果指定次數,他則只會切割指定次數後中止,另外您也可以使用選項進行其他設定.
以下為可使用的選項 :
PREG_SPLIT_NO_EMPTY
只傳回不為空值的項目
PREG_SPLIT_DELIM_CAPTURE
parenthesized expression in the delimiter pattern will be captured and returned as well. This flag was added for 4.0.5.
PREG_SPLIT_OFFSET_CAPTURE
取得相符合的字串位置,此函數將切割為二維陣列,
其中二維的第一個值為字串,第二個值是切割點的位置.
. 此選項發行於 PHP 4.3.0 中.
<?php
// split the phrase by any number of commas or space characters,
// which include " ", \r, \t, \n and \f
$keywords = preg_split ("/[\s,]+/", "hypertext language, programming");
?> |
<?php
$str = 'string';
$chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
print_r($chars);
?> |
<?php
$str = 'hypertext language programming';
$chars = preg_split('/ /', $str, -1, PREG_SPLIT_OFFSET_CAPTURE);
print_r($chars);
?> |
will yield
Array
(
[0] => Array
(
[0] => hypertext
[1] => 0
)
[1] => Array
(
[0] => language
[1] => 10
)
[2] => Array
(
[0] => programming
[1] => 19
)
)
|
http://www.php5.idv.tw/modules.php?mod=books&act=show&shid=711
|