11

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("#\u200e#u", '', $s);
$s = preg_replace("#\u200e#", '', $s);

Any help is appreciated!

Marc
  • 111
  • 1
  • 1
  • 3

6 Answers6

14

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);
Tim Groeneveld
  • 8,739
  • 3
  • 44
  • 60
7

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);
tmont
  • 2,562
  • 20
  • 15
0

try this

preg_replace('/\x{E2}\x{80}\x{8E}/', '', $s); 
// strip unicode chars (LEFT_TO_RIGHT_MARK) 
fancyPants
  • 50,732
  • 33
  • 89
  • 96
Shadi Abu Hilal
  • 281
  • 1
  • 3
  • 7
0

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

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
0

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 ?

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
0

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);
YOU
  • 120,166
  • 34
  • 186
  • 219