Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将句子中的第三个空格替换为\n_Php_String_Replace_Newline - Fatal编程技术网

Php 如何将句子中的第三个空格替换为\n

Php 如何将句子中的第三个空格替换为\n,php,string,replace,newline,Php,String,Replace,Newline,如何用以下句子中的“\n”替换第三个空格和后续空格 请在课程结束后提问 试试这个,应该管用 <?php $sentence = 'Some really long sentence without any kind of sense'; $se = explode(' ', $sentence); $s = ''; $i = 0; while ($i < count($se)) { $i++; $s .= $se[$i-1]; if ($i !== co

如何用以下句子中的
“\n”
替换第三个空格和后续空格

请在课程结束后提问

试试这个,应该管用

<?php

$sentence = 'Some really long sentence without any kind of sense';

$se = explode(' ', $sentence);
$s = '';

$i = 0;
while ($i < count($se)) {
    $i++;
    $s .= $se[$i-1];
    if ($i !== count($se)) {
        if ($i%3 == 0) {
            $s .= '\n';
        } else {
            $s .= ' ';
        }
    }
}

echo $s;

在这里,正则表达式是一个很好的选择,因为它不需要对输入字符串进行任何“切分”来运行迭代操作,而且一旦你理解了“魔力”,它就是一个相当简单的表达式。如果这是我的项目,我将使用regex而不是非regex方法,因为它是一个直接而简洁的一行程序,而任何性能损失对于最终用户来说都是完全不可察觉的,因为给定了示例输入字符串

代码:()

输出:

string(54) "Please ask your
questions
after
the
session
is
finish
"
Please ask your
questions
after
the
session
is
finished.


*如果输入字符串中没有可替换的限定空格,输入字符串将根据需要保持不变,并且不会生成错误/警告/通知。

用换行符替换第三个空格和连续空格的一种方法是获取第三个出现的字符串中的偏移量,并使用该偏移量替换字符

要获取可能使用的偏移量,请使用
\h+
匹配一个或多个水平空白字符,并使用
PREG\u offset\u CAPTURE
标志。(来自文档)如果传递了此标志,则对于发生的每个匹配,也将返回附加字符串偏移量

替换可能使用的字符

对于
start
参数,使用
$matches[0][2][1]
,对于长度参数,使用
$matches[0][2][0]
。索引0是返回的数组,索引2是第三个匹配项,索引0包含匹配项,索引1包含偏移量

$str = "Please ask your questions after the session is finished.";
preg_match_all('/\h+/', $str,$matches, PREG_OFFSET_CAPTURE);
$str = substr_replace($str, "\n", $matches[0][2][1], strlen($matches[0][2][0]));
echo $str;
将导致:

Please ask your
questions after the session is finished.

发布您的输入、预期输出和您的代码…还可以告诉我们您的代码的问题,如代码的输出所示。是在后每隔一个空格,还是在后每隔三个空格?模棱两可me@juan对于输入字符串的可变性和期望的输出,似乎存在一些争议。请澄清你的问题。这里有一条新的尾随线。您可以修剪结果以删除。请检查您的输出:
string(56)“请在会话结束后询问\n您的问题”
“尝试此”回答在StackOverflow上的值较低。每一个答案都要包含一个解释。你是否增加了这个问题的复杂性?如果这是你的项目,你真的会使用这个解决方案吗?@mickmackusa我把这个问题理解为用
\n
替换第三个空格和后续空格。我承认术语中存在一些歧义,但你修改了示例输入字符串——提供的唯一上下文。你是对的,我把它改为指出连续的空格。我已经把原来的例子放回了我的答案中。
~          #pattern delimiter
(?:        #start of non-capturing group
  ^        #match start of string
  \S+ \S+  #match one or more visible characters, a literal space, one or more visible characters, a literal space
  |        #or
  \G(?!^)  #continue from last match, but not the start of the string
)          #end of non-capturing group
\S+        #match one or more visible characters
\K         #restart the fullstring match (ignore previously matched characters)
           #match a literal space  (the only replaced character)
~          #pattern delimiter
$str = "Please ask your questions after the session is finished.";
preg_match_all('/\h+/', $str,$matches, PREG_OFFSET_CAPTURE);
$str = substr_replace($str, "\n", $matches[0][2][1], strlen($matches[0][2][0]));
echo $str;
Please ask your
questions after the session is finished.