www.webdeveloper.com
Results 1 to 7 of 7

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

  1. #1
    Join Date
    Oct 2012
    Posts
    4

    Question 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

    //Which list?
    if ( ($dia) == "Jueves" ) {
    $file "ListaJueves.txt";
    } else if ( (
    $dia) == "Viernes" ) {
    $file "ListaViernes.txt";
    } else if ( (
    $dia) == "Sabado" ) {
    $file "ListaSabado.txt";
    } else if ( (
    $dia) == "Feriado" ) {
    $file "ListaFeriados.txt";
    } else { exit; }

    //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.

  2. #2
    Join Date
    Sep 2011
    Location
    Bristol, England, United Kingdom
    Posts
    192
    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);

    $counter 0;

    for(
    $i 0$i strlen($string); $i++)
    {
        if(
    $string[$i] == " " || $string[$i] == " ")
        {
             
    $counter++;
        }

        if(
    $counter == 2)
        {
            
    $counter 0;
        
            
    $string[$i] = "\n";
        }

    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.
    Last edited by George88; 10-03-2012 at 08:31 AM.

  3. #3
    Join Date
    Oct 2012
    Posts
    4
    That's something good to start. Useful indeed.
    Maybe I could also use
    PHP Code:
    explode() 
    to separate words and then every two words add the break. I'm not too familiar with that function so I didn't tried.

    I'll try with your piece of code and tell you how it goes. I could also count commas to add 2 to the counter so it adds the break to commas too.

    Anyway, thanks for your help. I really appreciate it.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,337
    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

    eBookworm.us

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,337
    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

    eBookworm.us

  6. #6
    Join Date
    Oct 2012
    Posts
    4
    Thanks NogDog! That just worked great! Helped me a lot.

    Thanks for all the help everyone. Problem solved. (Although a new one comes up every time. Life's like that)

  7. #7
    Join Date
    Oct 2012
    Posts
    4
    Oh, to make it work in my own code it looks like this:
    PHP Code:
    $mail_solicitante=$HTTP_POST_VARS['mail'];
    $dia=$HTTP_POST_VARS['dia'];
    $invitados=$HTTP_POST_VARS['invitados'];
    $list str_replace", ""\r\n"$invitados);
    $lowercase strtolower($list); //remover caps
    $string ucwords($lowercase); //capitalizar

    //---------
    $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); 

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles