2010-09-23 71 views
0

我想通過分隔符數組拆分字符串,並且還獲取反饋分隔符是什麼。拆分字符串並獲取分隔符作爲返回以及

實施例:
$mystring = 'test+string|and|hello+word';
$result = preg_split('/\+,|/+', $mystring);

我想的陣列作爲返回與像這樣
$return[0] = array('test','+');
$return[1] = array('string','|');

日Thnx預先

回答

3

以一個升OOK在爲preg_split()

編輯

例的PREG_SPLIT_DELIM_CAPTURE選項:

$mystring = 'test+string|and|hello+word'; 
$result = preg_split('/([\+|,])/', $mystring, null, PREG_SPLIT_DELIM_CAPTURE); 
+0

thnx馬克(回答和快速回復)! – Bokw 2010-09-23 10:22:46

0

我沒有寫我的回答之前瞭解PREG_SPLIT_DELIM_CAPTURE。這絕對比使用更清晰preg_match_all

<?php 
$s = 'a|b|c,d+e|f,g'; 
if (preg_match_all('/([^+,|]+)([+,|])*/', $s, $matches)) { 
    for ($i = 0; $i < count($matches[0]); $i++) { 
    echo("got '{$matches[1][$i]}' via delimiter '{$matches[2][$i]}'\n"); 
    } 
} 
相關問題