更换换行符UTF-8安全吗? [英] Is replacing a line break UTF-8 safe?

查看:73
本文介绍了更换换行符UTF-8安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有UTF-8字符串,并且想用HTML <br>替换换行符,这样安全吗?

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);

我知道str_replace不是UTF-8安全的,但是也许我可以解决这个问题.我问是因为没有mb_strreplace函数.

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.

推荐答案

UTF-8的设计目的是使多字节序列从不包含任何看起来像ASCII字符的内容.也就是说,每当遇到一个字节,该字节的值在0-127范围内时,您都可以放心地将其假定为ASCII字符.

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.

这意味着只要您仅尝试用ASCII字符替换ASCII字符,str_replace应该是安全的.

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

这篇关于更换换行符UTF-8安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆