Genadinik
Genadinik

Reputation: 18639

\n\r Not removed by PHP functions

On my home page, I display some info about the hikes (it is a hiking site) and embarassinlgy, the \r\n characters still show up.

I do these functons

$hike_description = htmlspecialchars ($hike_description);
$hike_description = nl2br($hike_description);

And still those characters do not go away. You can take a look yourself at http://www.comehike.com

Would you know what is the proper way to get rid of the \r\n characters? You can see it happening in the "Upcoming Hikes" section...on the 3rd hike.

Thanks!

Upvotes: 0

Views: 64

Answers (3)

Mr Griever
Mr Griever

Reputation: 4023

Try manually replacing the sequence

str_replace( "\r\n", "<br />", $hike_description );

Upvotes: 2

Ryan
Ryan

Reputation: 28177

In your case, the following will work:

$hike_description = str_replace ( "\\r\\n","<br />", $hike_description);

The text \r\n is literal, rather than control characters.

Upvotes: 3

Andrew Moore
Andrew Moore

Reputation: 95334

The \n\r in your page are not escape sequences... They are actually a \ character followed by a n character followed by a \ character followed by a r character.

In your database, you should store that as the actual characters, not the escape sequences. Then, calling nl2br() will work as expected.

Yes, you can do a str_replace(), however, you should instead fix the encoding of your data in your database. It will save you trouble in the future.

Upvotes: 1

Related Questions