我編寫並測試了這個自定義函數。試試看,讓我知道你的想法。
function fireSplit($str) {
if (strpos($str,"<") === FALSE) return explode(" ",$str);
$str = trim($str);
$out = array();
$curIdx = 0;
$endIdx = strlen($str) -1;
while ($curIdx <= $endIdx) {
if (substr($str,$curIdx,1) == " ") {
$curIdx += 1;
continue;
}
$nextspace = strpos($str," ",$curIdx);
$nexttag = strpos($str,"<",$curIdx);
$nexttag2 = strpos($str,"/",$nexttag);
$nexttag3 = strpos($str,">",$nexttag2);
if ($nextspace === FALSE) {
$out[] = substr($str,$curIdx);
$curIdx = $endIdx + 1;
continue;
}
if ($nexttag !== FALSE && $nexttag < $nextspace && $nexttag2 !== FALSE && $nexttag3 !== FALSE) {
$out[] = substr($str,$curIdx,($nexttag3 - $curIdx + 1));
$curIdx = $nexttag3 + 1;
} else {
$out[] = substr($str,$curIdx,($nextspace - $curIdx));
$curIdx = $nextspace;
}
}
return $out;
}
我叫:
fireSplit("one two <a href=\"haha\">three</a> four");
fireSplit("a <b>strong</b> c d e f");
,它返回:
array(4) {
[0]=>
string(3) "one"
[1]=>
string(3) "two"
[2]=>
string(24) "<a href="haha">three</a>"
[3]=>
string(4) "four"
}
array(6) {
[0]=>
string(1) "a"
[1]=>
string(13) "<b>strong</b>"
[2]=>
string(1) "c"
[3]=>
string(1) "d"
[4]=>
string(1) "e"
[5]=>
string(1) "f"
}
我正在寫一個函數,這個..應該就會完成.. – Fosco 2010-07-09 13:26:33