PHP utf8_encode()将空格转换为不间断空格 [英] PHP utf8_encode() converts spaces to non-breaking spaces
问题描述
非常简单:utf8_encode($ string)用不间断空格("\ u00a0")替换常规空格.我尝试使用str_replace过滤结果:
Perfectly simple: utf8_encode($string) replaces regular spaces with non-breaking spaces ("\u00a0"). I tried filtering the result with str_replace:
str_replace("\u00a0", " ", utf8_encode($string))
但这并不能解决问题.
igh,我是个白痴. utf8_encode()也不是问题.我以为我在使用该功能,忘记了在代码中禁用它.我的数据正在通过json_encode()进行AJAX请求. json_encode()有问题吗?我担心我可能会因为滥用堆栈溢出而感到内gui.我将尝试使用Google搜索.
Sigh, I'm an idiot. It's not a problem with utf8_encode() either. I thought I was using that function, forgot I disabled it in my code. My data is being run through json_encode() for an AJAX request. Is it a problem with json_encode()? I worry I may be guilty of abusing Stack Overflow. I'll try Googling it.
最终数据本身存在问题,这些数据已从Word文档复制到MySQL表中.所有空格都被复制为不间断空格.很抱歉浪费大家的时间.
FINAL Problem was with the data itself, which was copied from a Word document into a MySQL table. All the spaces were copied as non-breaking spaces. Sorry for wasting everyone's time.
推荐答案
str_replace("\ u00a0",",utf8_encode($ dat)).但这并不能解决问题.
str_replace("\u00a0", " ", utf8_encode($dat)). But that didn't fix it.
PHP仅具有字节字符串,而没有本机Unicode字符串;因此,没有\u
转义,您实际上是在要求它转换输入中的反斜杠-u序列.
PHP only has byte strings, not native Unicode strings; consequently there is no \u
escape and you were asking it literally to convert backslash-letter-u sequences in the input.
要摆脱不间断的空格字符,您必须替换掉\xA0
(如果对传递给utf8_encode
之前可能拥有的ISO-8859-1数据进行了处理)或\xC2\xA0
(如果完成了)转码为UTF-8后.
To get rid of non-breaking space characters you would have to replace away \xA0
(if done over the ISO-8859-1 data you presumably have before passing to utf8_encode
), or \xC2\xA0
(if done after transcoding to UTF-8).
utf8_encode
仅将ISO-8859-1转码为UTF-8,它不接触空格,因此我怀疑您的实际数据中包含不间断的空格字符.
utf8_encode
only transcodes ISO-8859-1 to UTF-8, it doesn't touch spaces, so my suspicion is you have non-breaking space characters in your actual data.
这篇关于PHP utf8_encode()将空格转换为不间断空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!