如何在php中从字符串中去除unicode字符(从左到右)
在将字符串编码为JSON之前,我试图从字符串中删除从左到右的标记(\u200e)和从右到左的标记(\u200f)。以下两项似乎都不起作用:如何在php中从字符串中去除unicode字符(从左到右),php,regex,utf-8,Php,Regex,Utf 8,在将字符串编码为JSON之前,我试图从字符串中删除从左到右的标记(\u200e)和从右到左的标记(\u200f)。以下两项似乎都不起作用: $s = mb_ereg_replace("\u200e", '', $s); $s = preg_replace("#\u200e#u", '', $s); $s = preg_replace("#\u200e#", '', $s); 感谢您的帮助 您是否尝试过用UTF-8编码您的脚本文件,并在其中输入(或复制+粘贴)字符 如何使用str\u repla
$s = mb_ereg_replace("\u200e", '', $s);
$s = preg_replace("#\u200e#u", '', $s);
$s = preg_replace("#\u200e#", '', $s);
感谢您的帮助 您是否尝试过用UTF-8编码您的脚本文件,并在其中输入(或复制+粘贴)字符 如何使用
str\u replace
,并使用字符代码对该字符进行编码;也许是这样的:
$new_string = str_replace("\x20\x0f", "", $your_string);
而且,在您的情况下,由于要替换多个不同的字符,您可以在一次调用str\u replace
中替换所有字符:
$new_string = str_replace(
array(
"\x20\x0e",
"\x20\x0f",
),
array(
"",
"",
),
$your_string
);
它能解决您的问题吗?您的Unicode转义错误,应该可以:
preg_replace('/\x20(\x0e|\x0f)/', '', $string)
测试:
你能试试这个吗?200e和200f的utf8编码
$s=preg_replace('/\xe2\x80[\x8e\x8f]/', '', $s)
或者用str_替换
$s=str_replace("\xe2\x80\x8e", "", $s);
$s=str_replace("\xe2\x80\x8f", "", $s);
试试这个
preg_replace('/\x{E2}\x{80}\x{8E}/', '', $s);
// strip unicode chars (LEFT_TO_RIGHT_MARK)
经过几天的努力,我终于找到了答案
$str = preg_replace('/(\x{200e}|\x{200f})/u', '', $str);
字符串使用哪种编码?实际上,它确实有效。我的考试考虑不周。更新答案以包含str_replace()。可能也可以使用strtr()。\x0e到底是什么?@iankit
\x0e
是正则表达式等价于chr(0x20)
对我有效,而@tmont的答案(票数较高)对我无效
preg_replace('/\x{E2}\x{80}\x{8E}/', '', $s);
// strip unicode chars (LEFT_TO_RIGHT_MARK)
$str = preg_replace('/(\x{200e}|\x{200f})/u', '', $str);