NYCPHP Meetup

NYPHP.org

[nycphp-talk] Adding <br> and <p> to a text

Emmanuel. M. Decarie emm at scriptdigital.com
Thu May 22 12:28:14 EDT 2003


Hello chris,

>After you determine each paragraph, would it work to call nl2br($item) ?
>(This would also give you xhtml-compliant <br />s.)
>
>file:///usr/local/share/doc/php/function.nl2br.html
>
>     chris.

Ah! PHP surprise me everyday with 'out of the box' solutions. Thanks 
a lot for the pointer.

I made some mods to my function and its working as expected.

   function formateTexte ($texte, $ajouteParagraphes=1) {

       $texte = trim ($texte);

       # translate accented chars to html entities
       # keep html tags
       $table = get_html_translation_table (HTML_ENTITIES);
       $table['<'] = '<';
       $table['>'] = '>';
       $texte = strtr ($texte, $table);
      
       if ($ajouteParagraphes) {
         # add <p>
         $texteElements = preg_split ("/\
(\\s)+/U", $texte);
         $nouveauTexte = "";
         foreach ($texteElements as $item) {
           #be sure to strip control chars
           $item = trim ($item);
           if ( strlen ($item) != 0 ) {
             $nouveauTexte .= "<p>$item</p>";
           }
         }
         # add <br /> and 2 newlines after </p>
         $table = null;
         $texte = nl2br($nouveauTexte);
         $table["</p>"]    = "</p>\
\
";
         $texte        = strtr ($texte, $table);
       }
       return $texte;
   }

This list rocks!

Cheers
-Emmanuel
-- 
______________________________________________________________________
Emmanuel Décarie / Programmation pour le Web - Programming for the Web
Radio UserLand/Frontier - Perl - PHP - Javascript  <http://scriptdigital.com/>
Blog: <http://blog.scriptdigital.com> - AIM: scriptdigital




More information about the talk mailing list