PHP Convert Unicode to text

Viewed 43

I am receiving from a form the following urlencoded string %F0%9D%90%B4%F0%9D%91%99%F0%9D%91%92%F0%9D%91%97%F0%9D%91%8E%F0%9D%91%9B%F0%9D%91%91%F0%9D%91%9F%F0%9D%91%8E

If I decode it I get the following formatted text:

Is there any way with PHP to get the plain "Alejandra" text from the encoded or decoded string? I have tried without success several ways to do it with

mb_convert_encoding($string, "UTF-16",mb_detect_encoding($string))

iconv('utf-16', 'utf-8', rawurldecode($string)

and any other solution I could in stackoverflow.

Thank you in advance

3 Answers

urldecode or rawurldecode is sufficient.

$string = "%F0%9D%90%B4%F0%9D%91%99%F0%9D%91%92%F0%9D%91%97%F0%9D%91%8E%F0%9D%91%9B%F0%9D%91%91%F0%9D%91%9F%F0%9D%91%8E";
$str = urldecode($string);
var_dump($str);
//string(36) ""

Demo: https://3v4l.org/OMQ35

A special debugger gives me: string(36) UTF-8mb4. This means that there are also UTF-8 characters in the string that require 4 bytes. The character A is the Unicode character “” (U+1D434).

Note:

If the special UTF-8 characters cause problems, you can try to display the strings as ASCII characters with iconv.

$strAscii = iconv('UTF-8','ASCII//TRANSLIT',$str);  
//string(9) "Alejandra"

What you are getting is called a "psuedo-alphabet", you can see a list of them here: https://qaz.wtf/u/convert.cgi. The one that you appear to be getting can be seen here: https://en.wikipedia.org/wiki/Mathematical_Alphanumeric_Symbols

Basically what you need to do is take the string, split it and use a lookup table to convert it back to regular characters. This implementation is terribly efficient but that's because I grabbed the alphabets from the above Wikipedia page and was too lazy to reorganise it.

function math_symbols_to_plain_text($input, $alphabet)
{
    $alphabets = [
        ['a','','','','','','','','','','','','',''],
        ['b','','','','','','','','','','','','',''],
        ['c','','','','','','','','','','','','',''],
        ['d','','','','','','','','','','','','',''],
        ['e','','','','','','','','ℯ','','','','',''],
        ['f','','','','','','','','','','','','',''],
        ['g','','','','','','','','ℊ','','','','',''],
        ['h','','ℎ','','','','','','','','','','',''],
        ['i','','','','','','','','','','','','',''],
        ['j','','','','','','','','','','','','',''],
        ['k','','','','','','','','','','','','',''],
        ['l','','','','','','','','','','','','',''],
        ['m','','','','','','','','','','','','',''],
        ['n','','','','','','','','','','','','',''],
        ['o','','','','','','','','ℴ','','','','',''],
        ['p','','','','','','','','','','','','',''],
        ['q','','','','','','','','','','','','',''],
        ['r','','','','','','','','','','','','',''],
        ['s','','','','','','','','','','','','',''],
        ['t','','','','','','','','','','','','',''],
        ['u','','','','','','','','','','','','',''],
        ['v','','','','','','','','','','','','',''],
        ['w','','','','','','','','','','','','',''],
        ['x','','','','','','','','','','','','',''],
        ['y','','','','','','','','','','','','',''],
        ['z','','','','','','','','','','','','',''],
        ['A','','','','','','','','','','','','',''],
        ['B','','','','','','','','ℬ','','','','',''],
        ['C','','','','','','','','','','ℭ','','','ℂ'],
        ['D','','','','','','','','','','','','',''],
        ['E','','','','','','','','ℰ','','','','',''],
        ['F','','','','','','','','ℱ','','','','',''],
        ['G','','','','','','','','','','','','',''],
        ['H','','','','','','','','ℋ','','ℌ','','','ℍ'],
        ['I','','','','','','','','ℐ','','ℑ','','',''],
        ['J','','','','','','','','','','','','',''],
        ['K','','','','','','','','','','','','',''],
        ['L','','','','','','','','ℒ','','','','',''],
        ['M','','','','','','','','ℳ','','','','',''],
        ['N','','','','','','','','','','','','','ℕ'],
        ['O','','','','','','','','','','','','',''],
        ['P','','','','','','','','','','','','','ℙ'],
        ['Q','','','','','','','','','','','','','ℚ'],
        ['R','','','','','','','','ℛ','','ℜ','','','ℝ'],
        ['S','','','','','','','','','','','','',''],
        ['T','','','','','','','','','','','','',''],
        ['U','','','','','','','','','','','','',''],
        ['V','','','','','','','','','','','','',''],
        ['W','','','','','','','','','','','','',''],
        ['X','','','','','','','','','','','','',''],
        ['Y','','','','','','','','','','','','',''],
        ['Z','','','','','','','','','','ℨ','','','ℤ']
    ];

    $replace = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];

    $lookup = [
        'serif-normal',
        'serif-bold',
        'serif-italic',
        'serif-bolditalic',
        'sans-normal',
        'sans-bold',
        'sans-italic',
        'sans-bolditalic',
        'script-normal',
        'script-bold',
        'franktur-normal',
        'fraktur-bold',
        'monospace',
        'doublestruck'
    ];

    $map_index = array_search($alphabet, $lookup);

    $split = mb_str_split($input);

    $output = '';
    foreach ($split as $char) {
        foreach ($alphabets as $i => $letter) {
            if ($letter[$map_index] === $char)
                $output .= $replace[$i];
        }
    }

    return $output;
}

$input = '';
$output = math_symbols_to_plain_text($input, 'serif-italic');

echo $input . PHP_EOL . $output . PHP_EOL;

Yields:


Alejandra

If I am not wrong, you are trying to decode URL then why you are not trying to use urldecode() follow this .PHP DOC

Related