修剪空白ASCII字符“ 194”从字符串 [英] Trim whitespace ASCII character "194" from string
问题描述
最近遇到一个非常奇怪的问题,我的数据库包含的字符串看起来像是普通的空白字符,但实际上是其他字符。
Recently ran into a very odd issue where my database contains strings with what appear to be normal whitespace characters but are in fact something else.
例如,应用 trim()
到字符串:
"TEST "
让我:
"TEST "
。因此,我将最后一个字符复制并粘贴到字符串中:
as a result. So I copy and paste the last character in the string and:
echo ord(' ');
194
194?根据ASCII表,应为┬
。所以我现在很困惑。为什么这个字符似乎是空格,当 trim()
失败时,如何像这样的 trim()
字符呢?
194? According to ASCII tables that should be ┬
. So I'm just confused at this point. Why does this character appear to be whitespace and how can I trim()
characters like this when trim()
fails?
推荐答案
您可以尝试:
PHP trim
$foo = "TEST ";
$foo = trim($foo);
PHP str_replace
$foo = "TEST ";
$foo = str_replace(chr(194), '', $foo);
重要提示:您可以尝试使用
chr( 194).chr(160)
或'\u00A0'
PHP preg_replace
$foo = "TEST ";
$foo = preg_replace('#(^\s+|\s+$)#', '', $foo);
或(我不确定是否会很好)
$foo = "TEST ";
$foo = preg_replace('#[\xC2\xA0]#', '', $foo);
这篇关于修剪空白ASCII字符“ 194”从字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!