I'm trying to get some post data formatted for output on a browser and I can't use pre tags becuase the width attribute isn't supported anymore and it doesn't really work. I also need to break up a word if it's over 80 characters long, if someone's been a pillock basically and posted some crap. However, if you could advise on that it would be great, but the real problem is I can't seem to replace the newlines with brs. Here's the code:

$bad  = array("<",">","&lt;","&gt;");
$good = array("[","]","[","]");

preg_replace( "/[\r\n]+/" , "<br />" , str_replace( $bad , $good , mysql_real_escape_string( $_POST['post'] ) ) )

Any help?
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
LOL it looks like the owner of this site needs some help with the formatting too!

Where's my off the cuff insult for being so stupid and one letter solution? Is this cos I outed a measly formatting error with the site. Come on!!!!

Member Avatar for iamthwee

> but the real problem is I can't seem to replace the newlines with brs.

Honey why ain't you using nl2br();

>I also need to break up a word if it's over 80 characters long

Find out what the max no. of letters you can have per line. Then perform tricks using the modulus operator. You understand that honey?

>LOL it looks like the owner of this site needs some help with the formatting too!

She's too lazy to fix that.

Changed it to

nl2br(str_replace($bad,$good,mysql_real_escape_string($_POST['Post'])))

but still no <br /> tags are ending up in the database. The POST is coming from a textarea, if this helps...

Also, the modulus thing, no I don't get. Is there a way to print a string one letter at a time in a for loop and you go

if($iterator % 80 == 0) {echo "<br />"}

i just made a code to break up the words for you. you will need to customize it to work with you setup. i can do this for you but you need to post all of your code so i can see exactly what you are doing.

$qry = "SELECT Post FROM POST WHERE ID = " . $_GET['threadID'];
$rset = mysql_query($qry);
$row = mysql_fetch_array($rset);
echo $row[0];

I haven't really looked at the problem with a word being too long yet. I assume the below is the fix:

for($i=0;$i<strlen($row[0]);$i+=80){
echo substr($row[0],$i,80);
echo "\n";
}

However, it's problem that passing the post from a textarea to nl2br() isn't adding the br tags that's really getting me depressed.

I read this on a forum somewhere:

Either your clean_var or mysql_less_safe functions are probably escaping the \r and \n characters using addslashes or mysql_real_escape_string. If that's the case, you'd need to do a str_replace for "\\\r\\\n" (or something like that I've never really learned how to count up all the slashes -- echo out the $new_course_poc to see what they are being turned into)

I guess this means that you just need to do the nl2br() after the tags are removed and before the mysql_escape_string() is called. I'm at work at the moment so I can't test my theory, but maybe is the solution.

Also, you'd need something like:

if (instr("<br />", substr($row[0],$i,80))){
$i+= 80-(instr("<br />",substr($row[0],$i,80))+5);
}

In that fix I put up to stop you wrapping half way through a line for which the previous line had a break.

But yeah, I haven't tested any of this, it's just food for thought.

so if am reading this right, you want to add <br /> before it goes into the database. from the code you wrote it is adding <br /> when its being retrieved from the database

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.