tags:

views:

318

answers:

4

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!

A: 

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

Pekka
A: 

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
+3  A: 

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
Why doesn't `str_replace` work?
Asaph
actually, it does work. my test was ill conceived. updated answer to include str_replace(). could probably use strtr() as well.
tmont
A: 

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);
S.Mark