tags:

views:

92

answers:

3

If I have a UTF-8 string and want to replace line breaks with the HTML
, 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.

+5  A: 

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

Btw, you could also look at the nl2br()

zerkms
A: 

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.

b_i_d
+3  A: 

UTF-8 is designed so that multi-byte sequences never contain an anything that looks like an ASCII-character. That is, any time you encounter a byte with a value in the range 0-127, you can safely assume it to be an ASCII character.

And that means that as long as you only try to replace ASCII characters with ASCII characters, str_replace should be safe.

jalf
* Only try to replace ASCII characters with ASCII characters.
Potatoswatter
+1 for the proper explanation. I think others tried to say the same thing, but this is the only answer that made perfect sense to me.
musicfreak
@Potatoswatter: oops yeah, that's a pretty important point. Edited. ;)
jalf