开发者

Getting rid of \r\n strings

I have a form into which I entered a newline character which looked correct when I entered it, but when the data is now pulled from the da开发者_JAVA百科tabase, instead of the white space, I get the \n\r string showing up.

I try to do this:

$hike_description = nl2br($hike_description);

But it doesn't work. Does anyone know how this can be fixed? I am using PHP. And here is the page where this is happening. See the description section of the page: http://www.comehike.com/hikes/scheduled_hike.php?hike_id=130

Thanks, Alex


Does anyone know how this can be fixed?

Sure. Your code doing unnecessary escaping, most likely before adding text to the database.
So, instead of replacing it back, you have to find that harmful code and get rid of it.


This means, you have probably plain text '\n\r' strings in the db.

Try to sanitize db output before display:

$sanitized_text = preg_replace('/\\[rn]/','', $text_from_db);

(just a guess).

Addendum:

Of course, as Col. Shrapnel pointed out, there's something fundamentally wrong with the contents of the database (or, it is used this way by convention and you don't know that).

For now, you have fixed a symptom partially but it would be much better to look for the reason for these escaped characters being in the database at all.

Regards

rbo


You can use str_replace to clean up the input.

$hike_description = nl2br(str_replace("\r\n", "\n", $hike_description));


$hike_description = str_replace(array('\n','\r'),'',$hike_description);

You may want to read up on the differences between the single quote and double quote in PHP as well: http://php.net/manual/en/language.types.string.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜