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.
This is what I got so far:
PHP Code:
$dia=$HTTP_POST_VARS['dia'];
$invitados=$HTTP_POST_VARS['invitados'…
$clean = ereg_replace("[^A-Za-z \n]", "", $invitados); //clean
$lowercase = strtolower($clean); //remove caps
$text = ucwords($lowercase); //capitalize
$lista = //here I should need the code that modifies the $text string and makes it a list
//Write the file
$fh = fopen($file, 'a') or die("cant open file");
fwrite($fh, "\n".$lista);
fclose($fh);
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);
I haven't tested this so it might not work as I expect. Another solution is just to add a "\n" to the end of the last name upon submission, negating the need for a solution such as this.
Just to give precedence to any commas, you could do:
PHP Code:
<?php
$text = "Gonzalo Novoa Gerardo Gaudio, Norma Lean, Carlos Lobieta";
$parts = preg_split('/\s*,\s*/', $text);
foreach($parts as $part) {
preg_match_all('/\S+\s+\S+/', $part, $names);
foreach($names[0] as $name) {
echo "$name<br />\n";
}
}
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Just to give precedence to any commas, you could do:
PHP Code:
<?php
$text = "Gonzalo Novoa Gerardo Gaudio, Norma Lean, Carlos Lobieta";
$parts = preg_split('/\s*,\s*/', $text); foreach($parts as $part) { preg_match_all('/\S+\s+\S+/', $part, $names); foreach($names[0] as $name) { echo "$name<br />\n"; } }
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
//--------- $parts = preg_split('/\s*,\s*/', $string); foreach($parts as $part) { preg_match_all('/\S+\s+\S+/', $part, $names); foreach($names[0] as $name) { $separate.= "$name\n"; } }
$lista = ereg_replace("[^A-Za-z \n]", "", $separate); //limpiar //---------- //Escribimos el archivo $fh = fopen($file, 'a') or die("no se puede abrir el archivo"); fwrite($fh, "\n".$lista); fclose($fh);
Bookmarks