我想每3 .
PHP - 後具體occurence分割字符串後特殊字符
例如字符串後,打破了一句:
$var = "ABC.ABC.ABC.ABC.ABC.ABC.ABC.ABC.";
預期的結果:
[0] = ABC. ABC. ABC
[1] = ABC. ABC. ABC
[2] = ABC. ABC.
我想每3 .
PHP - 後具體occurence分割字符串後特殊字符
例如字符串後,打破了一句:
$var = "ABC.ABC.ABC.ABC.ABC.ABC.ABC.ABC.";
預期的結果:
[0] = ABC. ABC. ABC
[1] = ABC. ABC. ABC
[2] = ABC. ABC.
你會想要使用explode()
函數,然後將它們添加回來。事情是這樣的:
$line = "ABC.ABC.ABC.ABC.ABC.ABC.ABC.ABC.";
$tempSplit = explode(".", $line);
$result;
for ($x = 0; $x < count($tempSplit); $x++)
{
$result[intval($x/3)] .= $tempSplit[$x] . ".";
}
然後,你必須搞清楚,如果你想在最終.
留與否。您可以使用substr()
功能刪除它:link。
試試這個
var = "ABC.ABC.ABC.ABC.ABC.ABC.ABC.ABC.";
$array=explode(".", $var);
$str="";
$count=0;
$arr="";
for($i=0;$i<sizeof($array);$i++){
$str[]=$array[$i];
$count++;
if($count>2){
$arr[$i]=implode(". ",$str);
$str="";
$count=0;
}
}
var_dump(array_values($arr));
輸出將被
array (size=3)
0 => string 'ABC. ABC. ABC' (length=13)
1 => string 'ABC. ABC. ABC' (length=13)
2 => string 'ABC. ABC. ' (length=10)
你可能想試試這個:
$test = "ABC.ABC.ABC.ABC.ABC.ABC.ABC.ABC.\n"
. "The quick brown fox jumps over the lazy dog.\n"
. "Lorem ipsizzle dolizzle you son of a bizzle amizzle, \n"
. "its fo rizzle adipiscing elit. The bizzle tellivizzle \n"
. "velizzle, gizzle volutpizzle, suscipizzle bow wow wow, \n"
. "owned vizzle, owned. Pellentesque bow wow wow tortor. \n"
. "Sizzle erizzle. Shizznit izzle dolor dapibus get down \n"
. "get down tempizzle yo. Maurizzle go to hizzle bizzle \n"
. "izzle. Da bomb izzle dawg. Pellentesque eleifend \n"
. "rhoncus dope. In sure break yo neck, yall shiz \n"
. "dictumst. Shiznit dapibus. Curabitizzle boom \n"
. "shackalack fo shizzle mah nizzle fo rizzle, mah \n"
. "home g-dizzle, pretizzle rizzle, mattizzle crackalackin, \n"
. "eleifend funky fresh, nunc. Shit suscipizzle. Integizzle \n"
. "sempizzle velit sed daahng dawg.";
$result = preg_split("%((?:[^\.]*?\.){3})%s", $test, -1,
PREG_SPLIT_NO_EMPTY + PREG_SPLIT_DELIM_CAPTURE);
echo "<pre>"; print_r ($result); echo "</pre>";
如果它的的確確是句子,他們通常用句號結尾(「。」),後跟一個空格(「」)。爲避免你的結果,這些空間您可以使用下面的正則表達式:
$result = preg_split("%((?:[^\.]*?\.\s?){3})%s", $test, -1,
PREG_SPLIT_NO_EMPTY + PREG_SPLIT_DELIM_CAPTURE);
,是它(離開週期之後的情況,因爲你的文字說的)或「第三時期」,「第3期後」(刪除期間,如你預期的結果所示)? – Amadan
我們不在SO上提供從頭開始的代碼。請查看[tour](http://stackoverflow.com/tour)和[如何提問](http://stackoverflow.com/help/how-to-ask) – moffeltje