Alex Rivera | Logout

Is replacing a line break UTF-8 safe?

Asked 2010-10-10T22:21:04.133
9

If I have a UTF-8 string and want to replace line breaks with the HTML <br> , is this safe?

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

I know str_replace isn't UTF-8 safe but maybe I can get away with this. I ask because there isn't an mb_strreplace function.

Edit
Report

2 Answers

7

str_replace() is safe for any ascii-safe character.

Btw, you could also look at the nl2br()

answered 2010-10-10T22:26:16.307
1

1st: Use the code-sample markup for code in your questions.

2nd: Yes, it is save.

3rd: It may not be what you want to archieve. This could be better:

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

Don't forget that different operating systems handle line breaks different. The code above should replace all line breaks, no matter where they come from.

answered 2010-10-10T23:00:40.990

Your Answer