Php 拆分字符串分隔符的每个已定义重复

Php 拆分字符串分隔符的每个已定义重复,php,string,Php,String,这是一根绳子。没有新的线路。我需要新线来做 段落。我决定使用跟随停止,即点后跟白色 每两个点出现一段的空间 绳子 在上面的伪字符串中,正如它所描述的那样,我需要在每次出现后都拆分字符串,然后再添加一个空格以添加\n或标记 我知道的唯一方法是分解,然后在数组中循环并在每两个元素之后继续。但是,它将拆分字符串,不管后面是否有空格 此外,此过程可能存在性能问题 我需要知道是否有一种更准确、更高效的方法 $arr = explode('.',$mystring); $output = ''; for (

这是一根绳子。没有新的线路。我需要新线来做 段落。我决定使用跟随停止,即点后跟白色 每两个点出现一段的空间 绳子

在上面的伪字符串中,正如它所描述的那样,我需要在每次出现
后都拆分字符串,然后再添加一个空格以添加
\n
标记

我知道的唯一方法是
分解
,然后在数组中循环并在每两个元素之后继续。但是,它将拆分字符串,不管后面是否有空格

此外,此过程可能存在性能问题

我需要知道是否有一种更准确、更高效的方法

$arr = explode('.',$mystring);
$output = '';
for ($i=0; $i < count($arr); $i++){
  $output .= $arr[$i].'. ';
  if (isset($arr[$i+1])){
   $output .= $arr[$i+1].'. '."\n";
   $i++;
  }
  else{
   $output .= '. '."\n";
  }
}
使用


用正则表达式的方法怎么样

$string = 'This is a string. Without any new lines. I need the newlines to make paragraphs. I decided to use follow-stop i.e the dot followed by white space to make paragraphs every two occurance of those dots in the string.';
preg_match_all('~((?:.+?\.){2})(.*)~', $string, $matches);
print_r($matches);
输出:

Array
(
    [0] => Array
        (
            [0] => This is a string. Without any new lines. I need the newlines to make paragraphs. I decided to use follow-stop i.e the dot followed by white space to make paragraphs every two occurance of those dots in the string.
        )

    [1] => Array
        (
            [0] => This is a string. Without any new lines.
        )

    [2] => Array
        (
            [0] =>  I need the newlines to make paragraphs. I decided to use follow-stop i.e the dot followed by white space to make paragraphs every two occurance of those dots in the string.
        )

)
Array
(
    [0] => This is a string.
    [1] => Without any new lines.
    [2] => I need the newlines to make paragraphs.
    [3] => I decided to use follow-stop i.e the dot followed by white space to make paragraphs every two occurance of those dots in the string.
)
Regex101演示:

0
是找到的整个表达式<代码>1
是第一个捕获的组(即前两个句子)<代码>2是剩余的内容

实际上,在重读问题时,你们是否试图在每一句话上分开?如果是这样,也许这就是你想要的:

$string = 'This is a string. Without any new lines. I need the newlines to make paragraphs. I decided to use follow-stop i.e the dot followed by white space to make paragraphs every two occurance of those dots in the string.';
preg_match_all('~(.+?[.?!])(?:\s+|$)~', $string, $matches);
print_r($matches[1]);
输出:

Array
(
    [0] => Array
        (
            [0] => This is a string. Without any new lines. I need the newlines to make paragraphs. I decided to use follow-stop i.e the dot followed by white space to make paragraphs every two occurance of those dots in the string.
        )

    [1] => Array
        (
            [0] => This is a string. Without any new lines.
        )

    [2] => Array
        (
            [0] =>  I need the newlines to make paragraphs. I decided to use follow-stop i.e the dot followed by white space to make paragraphs every two occurance of those dots in the string.
        )

)
Array
(
    [0] => This is a string.
    [1] => Without any new lines.
    [2] => I need the newlines to make paragraphs.
    [3] => I decided to use follow-stop i.e the dot followed by white space to make paragraphs every two occurance of those dots in the string.
)

要么使用“.”(点后跟空格)作为分解字符或正则表达式。@baao您的意思是使用
preg\u split
?如果是,count中是否有regex keep repeation?2不是该函数中的
int&$count
的有效参数。这不会提供所需的结果。它将替换每个
。但是,我需要每出现两次
就更换一次@worldofjr