Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is replacing a line break UTF-8 safe?

Tags:

php

utf-8

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.

like image 425
Jonny Barnes Avatar asked Oct 10 '10 22:10

Jonny Barnes


1 Answers

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.

like image 163
jalf Avatar answered Sep 23 '22 15:09

jalf