Page 1 of 1
string replace
Posted: Sat Aug 16, 2003 1:21 am
by nincha
say a string has "\n" in it. to be more specific --- a text area is submited with newlines breaks in it (by hitting the `enter` key). is there any way to replace those lines break with a html <br/> tag? if not, any one know how to parse xml files with a content that has line breaks in it?---->
ex:
<xmlTag> Info Info Info //line break
info info info info //line break
info info info</xmlTag>
because if i parse this right here, i will only get the top line of info.
if still confusing i can explain more. thnx
Posted: Sat Aug 16, 2003 5:40 am
by tylerdurden
Posted: Sat Aug 16, 2003 12:13 pm
by derek
You can use nl2br(). It's used because it returns <br/>.
But I personally suggest don't use nl2br() If you want to save data in when you save your data in database otherwise breaks <br> are also inserted in the database
Posted: Sat Aug 16, 2003 7:07 pm
by tylerdurden
But you could use nl2br after you pulled the data from the database and are preparing it for browser display.
Posted: Sat Aug 16, 2003 7:49 pm
by nigma
I actually just changed my forum so that instead of doing n12br() then storing data in mySQL it does it the way tylerdurden said.
Posted: Sat Aug 16, 2003 8:36 pm
by macewan
$text = preg_replace("/(\015\012)|(\015)|(\012)/","<br /> ",$text);
as it is being displayed to page. leave the carriage returns in the text as they are entered from the form and being placed into the database. at least this is what I do.
Posted: Sat Aug 16, 2003 10:16 pm
by nigma
Anyone know off hand whether nl2br() just does what macewan said ?
Like this:
Code: Select all
<?php
function nl2br($string)
{
$string = preg_replace("/(\015\012)|(\015)|(\012)/","<br /> ",$string);
return $string;
}
$text = "Blah\nBlahBlah\nBLAH!";
print nl2br($text);
?>