开发者

How to strip unicode chars (LEFT_TO_RIGHT_MARK) from a string in php

开发者 https://www.devze.com 2022-12-14 14:42 出处:网络
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:

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:

$s = mb_ereg_replace("\u200e", '', $s);
$s = preg_replace("#\u200开发者_运维知识库e#u", '', $s);
$s = preg_replace("#\u200e#", '', $s);

Any help is appreciated!


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


Your Unicode escaping is wrong, this should work:

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


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


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

$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 :

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

Does it work for your problem ?


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

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

or with str_replace

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


try this

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

精彩评论

暂无评论...
验证码 换一张
取 消