Re: Replace a space with a newline every 2 spaces
| From: | Andrew Ballard | Date: | Thu, 22 Apr 2010 17:56:48 +0000 |
| Subject: | Re: Replace a space with a newline every 2 spaces | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-304439@lists.php.net to get a copy of this message | ||
On Thu, Apr 22, 2010 at 1:29 PM, Paul Halliday <paul.halliday@gmail.com> wrote:
> Ex:
>
> "This is the string and it is this long"
>
> This is
> the string
> and it
> is this
> long
>
> I found some long functions to achieve this but I couldn't help but
> think that it could be done in a couple lines.
>
> Possible?
>
> Thanks.
>
I'm not sure it's the best method, but it didn't take long to come up with this:
<?php
$string = "This is the string and it is this long";
if (preg_match_all('/(?:(?:\w+\s+){2}|(?:\w+$))/', $string, $matches)) {
var_dump($matches[0]);
}
?>
Andrew