[thelist] PHP string tip...

john corry john at neoncowboy.com
Fri Aug 31 16:48:15 CDT 2001


Hi all!
Sorry for being so quiet lately, I've been busy for the last couple month or
so a) preparing to travel, b) traveling and c) making up for work I missed
while traveling :) It was pretty fun though, we got to go to Maria's in
Santa Fe for the best margarita's and combination plate in the world.

So I just solved a problem I've been turning over in my head for awhile and
thought I'd share my solution as a...

<tip type="php string to paragraph function">
Got a big wad of text from a database you want to turn into multiple
paragraphs? I do. MySQL stores the line breaks, but you lose them when you
convert it back to a string variable in your script. So, I wrote this handy
little function:

// make_p(string, $class="string")
/* takes a string and stuffs it into <p></p> tags, and also replaces all
carriage returns (chr(13)) with </p><p> tags. So you wind up with your
string neatly broken into paragraphs. Assign a class attribute to the
resulting <p>'s with the optional second argument, $class */

function make_p($string, $class=""){
if (isset($class)){
$p = "<p class = \"$class\">"}
else{
$p = "<p>";
}
 $string = str_replace( chr(13) , "</p>$p" , $string );
 $string = $p. $string. "</p>\n";
 return $string;
}
</tip>

Thanks :)
John Corry





More information about the thelist mailing list