Reputation: 2777
Is there a PHP function that can take a string and convert any special characters to unicode. Similar to htmlspecialchars()
or UTF8_encode()
.
For example in the string: "I think Bob's going too".
I would need the apostrophe or single right quote unicode in place of the apostrophe in "Bob's". So then after conversion the string should read: "I think Bob\u2019s going too".
I need this for use in a PHP script that prints into a javascript function.
Using \
to escape or '
does not work, it stops the script from running. I am trying to use Flowplayers Playist plugin. The only way it seems I can have a string with special characters is if they are in unicode.
Here is a JSFIDDLE to play around with and see what I mean when I say it doesn't work. Just replace \u2019
with '
or something similar and click to have the song play. The media player just goes black and doesn't play anything, whereas if you leave it with \u2019
then it plays fine.
Any help is appreciated.
Upvotes: 1
Views: 7228
Reputation: 1043
I think json_encode()
is the function you are looking for here.
The following code:
$string = "I think Bob’s going too";
print_r(json_encode($string));
will output:
"I think Bob\u2019s going too"
Upvotes: 2