2

I am trying to make a little program in Php that get a message from a text file (json) and to convert some parts into emojis.

The thing is, I just began php yesterday and since, I didn't find the solution to make it works...

I have this code :

$message = $_GET["message"];
$emojis = array();
echo "{$message}\n";
preg_match("({[A-Z0-9]+})", $message, $emojis);
foreach($emojis as $key => $value) {
    echo "{$key} => {$value}\n";
    $emoji_string = "\\u".$value;
    $emoji_unicode = utf8_encode($emoji_string);
    $message = str_replace($value, $emoji_unicode, $message);
}
echo "{$message}\n";

So what I am trying to achieve is, by using this regex (\\u{[A-Z0-9]+}), created on Regex101, I would like to transform a simple unicode text into a emoji in order to display a beautiful message haha

So far, I understand that \u{270B} #RaisedHand is only one char at the end, so that's why I try to convert my string into a simple char. Am I wrong? However, the code is wrong since I have "\\u{270B}" instead of "\u{270B}"...

I am really out of idea for the moment about that, any idea on your side?

Thanks for any help !

Max

Edit 1

I tried with this message: Hey what's up? {270B}

output:

PHP
debug start
Hey what's up? {270B}
0 => {270B}
Hey what's up? \u{270B}
debug done
Emixam23
  • 3,854
  • 8
  • 50
  • 107
  • What are you getting by echoing $message –  Apr 08 '18 at 21:40
  • I edited for you :) – Emixam23 Apr 08 '18 at 21:44
  • I may be very stupid but I think the problem is that \u{270B} is echoed as string and I had same problem some time long ago I may be able to find out how I did it. –  Apr 08 '18 at 21:50
  • I know it's encoded as string, what I am looking for is to encode it from string into a char :/ – Emixam23 Apr 08 '18 at 21:57
  • 1
    I posted answer what is working for me :D –  Apr 08 '18 at 22:00
  • 2
    `utf8_encode` is a confusing name for a function, because it only does one thing: Convert a ISO-8559-1 string (latin1) into UTF-8. It's almost never what you need, and definitely not in this case. – Evert Apr 08 '18 at 22:11

2 Answers2

1

Try this: I tested it its working for me

$unicodeChar = '\u1000';
echo json_decode('"'.$unicodeChar.'"');

From here

  • Accept answer if it's working for you :D I kinda like these green notifications :D –  Apr 08 '18 at 22:03
  • I couldn't make it work :/ but the other anwser made it work :/ Sorry :( Thanks anyway ! :) – Emixam23 Apr 08 '18 at 22:15
  • Well forgot to mention it I tried without **{}** but nevermind :D –  Apr 08 '18 at 22:16
  • Yeah :D ........ (don't know that to write so I added multiple dots to be able to send msg) –  Apr 08 '18 at 22:23
  • Anyway, the solution given doesn't work for all emojis... with this format `\u{1F525}`, it gives a fire, with `\u1F525` I get only `{1F525}` – Emixam23 Apr 08 '18 at 22:35
  • Yeah, me2. If the other guy solution works better try it :D You may try to import your own fonts as emojis and then call them :D –  Apr 08 '18 at 22:40
0

The problem is that \u {270B} doesn't exist. Right is \u270B. So do this:

<?php
$message = "message {270B}";
$emojis = array();
echo "{$message}\n";
preg_match("({[A-Z0-9]+})", $message, $emojis);
foreach($emojis as $key => $value) {
    echo "{$key} => {$value}\n";
    $emoji_string = "\u".substr ($value, 1, -1); //remove {}
    $emoji_unicode = json_decode('"'.$emoji_string.'"'); //decode
    $message = str_replace($value, $emoji_unicode,  $message);
}
echo "{$message}\n";

I tested it and it works. Output is: message ✋

mozkomor05
  • 1,367
  • 11
  • 21