如何在php中从字符串中删除unicode字符(LEFT_TO_RIGHT_MARK)

时间:2022-02-24 11:01:54

I'm trying to remove LEFT-TO-RIGHT-MARK (\u200e) and RIGHT-TO-LEFT-MARK (\u200f) from a string before encoding it as JSON. Neither of the following seems to work:

在将字符串编码为JSON之前,我尝试从字符串中删除从左到右的标记(\u200e)和从右到左的标记(\u200f)。以下两种方法似乎都不起作用:

$s = mb_ereg_replace("\u200e", '', $s);
$s = preg_replace("#\u200e#u", '', $s);
$s = preg_replace("#\u200e#", '', $s);

Any help is appreciated!

任何帮助都是赞赏!

6 个解决方案

#1


5  

Your Unicode escaping is wrong, this should work:

您的Unicode转义是错误的,这应该是有效的:

preg_replace('/\x20(\x0e|\x0f)/', '', $string)

Test:

测试:

<?php
  $string = chr(0x20) . chr(0x0e) . 'fo' . chr(0x20) . chr(0x0e) . 'o' . chr(0x20) . chr(0x0f);
  echo $string . "\n";
  echo preg_replace('/\x20(\x0e|\x0f)/', '', $string);
?>

Or, use str_replace():

或者,使用():大小写不敏感

  str_replace(array("\x20\x0e", "\x20\x0f"), '', $string);

#2


5  

After wrestling with this issue for a couple of days, I finally have found the answer!

在与这个问题纠缠了几天之后,我终于找到了答案!

$str = preg_replace('/(\x{200e}|\x{200f})/u', '', $str);

#3


0  

Have you tried encoding your script file in UTF-8, and actually typing (or copy+pasting) the characters in there?

您是否尝试过用UTF-8编码您的脚本文件,并实际输入(或复制+粘贴)其中的字符?

#4


0  

What about using str_replace, and coding that character using it's character codes ; something like this, maybe :

如何使用str_replace,并使用它的字符代码对该字符进行编码;也许是这样:

$new_string = str_replace("\x20\x0f", "", $your_string);

And, in your case, as you have several different characters to replace, you might replace them all in one call to str_replace :

在您的例子中,由于要替换几个不同的字符,您可以在一次调用str_replace中替换它们:

$new_string = str_replace(
    array(
        "\x20\x0e", 
        "\x20\x0f", 
    ),
    array(
        "", 
        "", 
    ),
    $your_string
);

Does it work for your problem ?

它对你的问题有效吗?

#5


0  

Could you try this? its utf8 encoding of 200e and 200f

你可以试试这个吗?它的utf8编码是200e和200f

$s=preg_replace('/\xe2\x80[\x8e\x8f]/', '', $s)

or with str_replace

或与str_replace

$s=str_replace("\xe2\x80\x8e", "", $s);
$s=str_replace("\xe2\x80\x8f", "", $s);

#6


0  

try this

试试这个

preg_replace('/\x{E2}\x{80}\x{8E}/', '', $s); 
// strip unicode chars (LEFT_TO_RIGHT_MARK) 

#1


5  

Your Unicode escaping is wrong, this should work:

您的Unicode转义是错误的,这应该是有效的:

preg_replace('/\x20(\x0e|\x0f)/', '', $string)

Test:

测试:

<?php
  $string = chr(0x20) . chr(0x0e) . 'fo' . chr(0x20) . chr(0x0e) . 'o' . chr(0x20) . chr(0x0f);
  echo $string . "\n";
  echo preg_replace('/\x20(\x0e|\x0f)/', '', $string);
?>

Or, use str_replace():

或者,使用():大小写不敏感

  str_replace(array("\x20\x0e", "\x20\x0f"), '', $string);

#2


5  

After wrestling with this issue for a couple of days, I finally have found the answer!

在与这个问题纠缠了几天之后,我终于找到了答案!

$str = preg_replace('/(\x{200e}|\x{200f})/u', '', $str);

#3


0  

Have you tried encoding your script file in UTF-8, and actually typing (or copy+pasting) the characters in there?

您是否尝试过用UTF-8编码您的脚本文件,并实际输入(或复制+粘贴)其中的字符?

#4


0  

What about using str_replace, and coding that character using it's character codes ; something like this, maybe :

如何使用str_replace,并使用它的字符代码对该字符进行编码;也许是这样:

$new_string = str_replace("\x20\x0f", "", $your_string);

And, in your case, as you have several different characters to replace, you might replace them all in one call to str_replace :

在您的例子中,由于要替换几个不同的字符,您可以在一次调用str_replace中替换它们:

$new_string = str_replace(
    array(
        "\x20\x0e", 
        "\x20\x0f", 
    ),
    array(
        "", 
        "", 
    ),
    $your_string
);

Does it work for your problem ?

它对你的问题有效吗?

#5


0  

Could you try this? its utf8 encoding of 200e and 200f

你可以试试这个吗?它的utf8编码是200e和200f

$s=preg_replace('/\xe2\x80[\x8e\x8f]/', '', $s)

or with str_replace

或与str_replace

$s=str_replace("\xe2\x80\x8e", "", $s);
$s=str_replace("\xe2\x80\x8f", "", $s);

#6


0  

try this

试试这个

preg_replace('/\x{E2}\x{80}\x{8E}/', '', $s); 
// strip unicode chars (LEFT_TO_RIGHT_MARK)