如何用 替换句子中的第3个空格

How can I replace the third space, and successive spaces, with " " in the following sentence?

Please ask your questions after the session is finished.

Try this, should work

<?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 .= '
';
        } else {
            $s .= ' ';
        }
    }
}

echo $s;
<?php
$str    = 'Please ask your questions after the session is finish';
$words  = explode(' ', $str);
$result = '';
foreach($words as $k => $word)
    $result .= $word . ($k<2 ? ' ' : "
");
var_dump($result);

Output:

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

Regex is an excellent choice here because it doesn't require any "chopping" at the input string to run iterated operations and once you wrap your head around the \G (continue anchor) "magic", it is a rather straightforward expression. If this was my project, I would use regex over a non-regex approach because it is a direct and concise one-liner while any performance losses will be completely unnoticeable to the end user given the sample input string.

Code: (Demo)

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

Output:

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

*In the event that there are no qualifying spaces to replace in the input string, the input string will remain unchanged as desired and no errors/warnings/notices will be generated.

One way to replace the 3rd whitespace and successive spaces with a newline is to get the offset in the string of the third occurrance and use that to replace the character.

To get the offset you might use preg_match_all, use \h+ to match one or more times a horizontal whitespace character and use the PREG_OFFSET_CAPTURE flag. (from the docs) If this flag is passed, for every occurring match the appendant string offset will also be returned.

To replace the character you might use substr_replace.

For the start parameter use $matches[0][2][1] and for the length parameter use $matches[0][2][0]. Index 0 is the returned array, index 2 is the third match, index 0 contains the match and index 1 contains the offset.

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

Will result in:

Please ask your
questions after the session is finished.

Demo