Replace a space with a newline every 2 spaces

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.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Paul Halliday [ Do, 22 April 2010 19:29 ] [ ID #2039946 ]

Re: Replace a space with a newline every 2 spaces

On Thu, Apr 22, 2010 at 1:29 PM, Paul Halliday <paul.halliday [at] 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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Andrew Ballard [ Do, 22 April 2010 19:56 ] [ ID #2039949 ]

Re: Replace a space with a newline every 2 spaces

Le 22/04/2010 19:29, Paul Halliday a écrit :

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

http://us.php.net/manual/en/function.wordwrap.php

--
Mickaël Wolff aka Lupus Michaelis
http://lupusmic.org

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Lupus Michaelis [ Fr, 23 April 2010 03:32 ] [ ID #2040103 ]
PHP » gmane.comp.php.general » Replace a space with a newline every 2 spaces

Vorheriges Thema: Updating cli executable on MS-Windows
Nächstes Thema: collect string from string