【问题标题】:How to replace the 3rd space in a sentence with \n如何用\n替换句子中的第三个空格
【发布时间】:2018-12-19 08:08:53
【问题描述】:

如何在下面的句子中用"\n" 替换第三个空格和连续的空格?

请在会议结束后提出您的问题。

【问题讨论】:

  • 发布您的输入、预期输出和您的代码...
  • 还告诉我们您的代码存在的问题,如您的代码输出所示。
  • 之后的每个空格,还是之后的每个第三个空格?对我来说模棱两可
  • @juan 似乎对您的输入字符串的可变性和您想要的输出存在一些争议。请澄清您的问题。

标签: php string replace newline


【解决方案1】:

正则表达式在这里是一个很好的选择,因为它不需要在输入字符串上进行任何“斩波”来运行迭代操作,并且一旦您将注意力集中在\G (continue anchor)“魔术”上,它就是一个相当简单的表达式。如果这是我的项目,我会使用正则表达式而不是非正则表达式方法,因为它是一种直接而简洁的单行方法,而在给定示例输入字符串的情况下,最终用户完全不会注意到任何性能损失。

代码:(Demo)

$string = "Please ask your questions after the session is finished.";
echo preg_replace('~(?:^\S+ \S+ |\G(?!^))\S+\K ~', "\n", $string);

输出:

Please ask your
questions
after
the
session
is
finished.

Pattern Demo

~          #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

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

【讨论】:

    【解决方案2】:

    用换行符\n 替换第三个空格和连续空格的一种方法是获取第三次出现的字符串中的偏移量并使用它来替换字符。

    要获得可能使用preg_match_all 的偏移量,请使用\h+ 匹配一个或多次水平空白字符并使用PREG_OFFSET_CAPTURE 标志。 (来自文档)如果传递了这个标志,对于每一个匹配,附加的字符串偏移量也将被返回。

    要替换您可能使用的字符substr_replace

    对于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.
    

    Demo

    【讨论】:

    • 您是否为这个问题增加了复杂性?如果这是您的项目,您真的会使用此解决方案吗?
    • @mickmackusa 我读这个问题是用\n替换第三个空格和连续的空格。
    • 我承认术语中有一些歧义,但您已经修改了示例输入字符串——提供的唯一上下文。
    • 你说得对,我改它是为了指出连续的空格。我已将原始示例放回我的答案中。
    【解决方案3】:

    试试这个,应该可以的

    <?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;
    

    【讨论】:

    • 请检查您的输出:string(56) "Please ask your\nquestions after the\nsession is finish "
    • “试试这个”答案在 StackOverflow 上的价值很低。始终在每个答案中附上解释。
    【解决方案4】:
    <?php
    $str    = 'Please ask your questions after the session is finish';
    $words  = explode(' ', $str);
    $result = '';
    foreach($words as $k => $word)
        $result .= $word . ($k<2 ? ' ' : "\n");
    var_dump($result);
    

    输出:

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

    【讨论】:

    • 这里有一个尾随的新行。您可以修剪结果以删除。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-18
    • 1970-01-01
    • 2019-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多