我想每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