糾正我,如果我錯了,但我不認爲你可以用一個簡單的正則表達式做到這一點。在一個完整的正則表達式的實現,你可以使用這樣的事情:
$parts = preg_split("/(?<!<[^>]*)\./", $input);
但是PHP不允許非固定長度的回顧後,這樣就不會工作。顯然只有2個是jgsoft和.net正則表達式。 Useful Page
我這個處理將是方法:
function splitStringUp($input, $maxlen) {
$parts = explode(".", $input);
$i = 0;
while ($i < count($parts)) {
if (preg_match("/<[^>]*$/", $parts[$i])) {
array_splice($parts, $i, 2, $parts[$i] . "." . $parts[$i+1]);
} else {
if ($i < (count($parts) - 1) && strlen($parts[$i] . "." . $parts[$i+1]) < $maxlen) {
array_splice($parts, $i, 2, $parts[$i] . "." . $parts[$i+1]);
} else {
$i++;
}
}
}
return $parts;
}
你沒有提到你想要什麼發生,當一個人一句話就是> 8000個字符長,所以這只是使他們完好。
輸出樣本:
splitStringUp("this is a sentence. this is another sentence. this is an html <a href=\"a.b.c\">tag. and the closing tag</a>. hooray", 8000);
array(1) {
[0]=> string(114) "this is a sentence. this is another sentence. this is an html <a href="a.b.c">tag. and the closing tag</a>. hooray"
}
splitStringUp("this is a sentence. this is another sentence. this is an html <a href=\"a.b.c\">tag. and the closing tag</a>. hooray", 80);
array(2) {
[0]=> string(81) "this is a sentence. this is another sentence. this is an html <a href="a.b.c">tag"
[1]=> string(32) " and the closing tag</a>. hooray"
}
splitStringUp("this is a sentence. this is another sentence. this is an html <a href=\"a.b.c\">tag. and the closing tag</a>. hooray", 40);
array(4) {
[0]=> string(18) "this is a sentence"
[1]=> string(25) " this is another sentence"
[2]=> string(36) " this is an html <a href="a.b.c">tag"
[3]=> string(32) " and the closing tag</a>. hooray"
}
splitStringUp("this is a sentence. this is another sentence. this is an html <a href=\"a.b.c\">tag. and the closing tag</a>. hooray", 0);
array(5) {
[0]=> string(18) "this is a sentence"
[1]=> string(25) " this is another sentence"
[2]=> string(36) " this is an html <a href="a.b.c">tag"
[3]=> string(24) " and the closing tag</a>"
[4]=> string(7) " hooray"
}
對不起!我忘記提及這一點。我將更新這一點。 – Sadi 2010-04-30 19:29:58
它看起來像你的解決方案放棄fullstops:P這將不會是問題添加完整站(我認爲):) – Sadi 2010-04-30 19:46:41
是啊,只是添加一個。到每個部分的末尾:) – oedo 2010-05-01 07:53:37