Convert hexadecimal codes to characters

Does PHP have a function that searches for hexadecimal codes in a string and converts them to its char equivalents?

For example, I have a line containing the following

"Hello World \ x19s"

And I want to convert it to

"Hello World"

Thanks in advance.

+3
source share
4 answers

This code converts "Hello World \ x27s" to "Hello World". It converts "\ x19" to the character "end of medium" since what 0x19 represents in ASCII.

$str = preg_replace('/\\\\x([0-9a-f]{2})/e', 'chr(hexdec($1))', $str);
+4
source

Correct me if I am wrong, but I think you should change the callback as follows:

$str = preg_replace('/\\\\x([0-9a-f]{2})/e', 'chr(hexdec(\'$1\'))', $str);

, '=' (\ x3d), .

+4

/e php- preg_replace_callback. :

preg_replace_callback('/\\\\x([0-9a-f]{2})/', function ($m) { return chr(hexdec($m[1])); },  $str );
+2

/ e The modifier causes PHP errors. It is deprecated with new PHP updates. The correct way to convert hex codes to characters is:

$str = html_entity_decode($str, ENT_QUOTES | ENT_XML1, 'UTF-8');

This will turn 'into ' and &into & etc.

0
source

Source: https://habr.com/ru/post/1777156/


All Articles