Ricky Mason
Ricky Mason

Reputation: 1828

removing \n from a string retrieved from DB

I have tried quite a few things so far with no luck:

I am using TinyMCE, which I run through mysql_real_escape_string() then add to database.

heres an example of the string thats stored in the DB:

<p>It would be nice to be able to put this in 2 categories....</p>\n<p>something to think about.</p>

I retrieve the data then I get problems. I can't get rid of the \n

$string = the database entry listed above

$string = substr($item['body'], 0, 120). "...";
$item['bodysum'] = nl2br(stripslashes(str_replace("\n", "<br />", $string)));

Here is a pic of the output.

enter image description here

I just want it to be normal HTML. If possible I'd like to convert it all to one line as well instead of making the section larger. Its supposed to be a summary of what someone posts, so having it make the summary area larger for 1 word then a new line doesn't make sense!

Upvotes: 1

Views: 155

Answers (3)

addiedx44
addiedx44

Reputation: 2743

Is that "\n" a literal "\" followed by "n"? If that's the case, then try:

$item['bodysum'] = nl2br(str_replace("\\n", "<br />", $string));

Upvotes: 1

Khawer Zeshan
Khawer Zeshan

Reputation: 9646

Try this

$text = '<p>It would be nice to be able to put this in 2 categories....</p>\n<p>something to think about.</p>';

echo preg_replace('#(\\\r|\\\r\\\n|\\\n)#', '<br/>', $text);

EXAMPLE HERE

Upvotes: 1

DBuTbKa
DBuTbKa

Reputation: 106

Try first strip_slashes than nl2br

$item['bodysum'] = nl2br(stripslashes($string));

Upvotes: 1

Related Questions