If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

How to add a linebreak in a string every two words?

I'm making a PHP script which makes a list of first and last names, taken from a form. And then saves it to a .txt file.
As several people is just placing one name after another instead of using line breaks.
I need to make a line break every 2 words manually.

As you can see, as it gets the $invitados string it gets formatted and removes non-Alphabetic characters, but then I would need to make my .txt list to look like a list.

Now for the real question:
How do I make a new line after every two words from a block of text?

It should make this:

Gonzalo Novoa Gerardo Gaudio, Norma Lean, Carlos Lobieta

to look like this

Gonzalo Novoa
Gerardo Gaudio
Norma Lean
Carlos Lobieta

Please note that it should replace the space after "Novoa" in the example, as well as the commas.
There's some annoying people who separates names with more characters than only commas or no comma at all.
Adding the linebreak after every two words is my main concern.

With respect to adding a line break every 2 words, you could write a simple solution which increments a counter by 1 each time a space is encountered, and just add a line break to the string after the 2nd space is found. Perhaps something along the lines of:

PHP Code:

$string = "Gonzalo Novoa Gerardo Gaudio, Norma Lean, Carlos Lobieta";

// Can't recall if this is a global S&R.$string = str_replace(",", "", $string);