php-在特定出现后按特殊字符拆分字符串
我想在每三次之后破译一个句子php-在特定出现后按特殊字符拆分字符串,php,string,Php,String,我想在每三次之后破译一个句子 示例字符串: $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); $r
示例字符串:
$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] . ".";
}
$line=“ABC.ABC.ABC.ABC.ABC.ABC.ABC.ABC.”;
$tempSplit=分解(“.”,$line);
$result;
对于($x=0;$x
然后你必须弄清楚你是否希望最后的
留下来。您可以使用substr()
函数将其删除。试试这个
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));
您可能希望尝试以下方法:
$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>";
是“在第三个周期之后”(如您的文本所示,将该周期留到后面)还是“在第三个周期上”(如您预期的结果所示,删除该周期)?我们在此不提供从头开始的代码。请看一下和
$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);