Reputation: 2285
I want to remove ZERO WIDTH NON-JOINER character from a string but using str_replace
wasn't useful.
Upvotes: 3
Views: 4450
Reputation: 8743
str_replace will do what you want, but PHP does not have very good native support for Unicode. The following will do what you ask. json_decode has been used to get the Unicode char, since PHP does not support the \u syntax.
<?php
$unicodeChar = json_decode('"\u200c"');
$string = 'blah'.$unicodeChar.'blah';
echo str_replace($unicodeChar, '', $string);
?>
edit: While my method works, I would suggest you use fiskfisk's solution. It is less hacky than using json_decode.
Upvotes: 0
Reputation: 52872
str_replace should solves this, as long as you're careful with what you're replacing.
// \xE2\x80\x8C is ZERO WIDTH NON-JOINER
$foo = "foo\xE2\x80\x8Cbar";
print($foo . " - " . strlen($foo) . "\n");
$foo = str_replace("\xE2\x80\x8C", "", $foo);
print($foo . " - " . strlen($foo) . "\n");
Outputs as expected:
foobar - 9
foobar - 6
Upvotes: 7