Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Hex Codes into Characters

Tags:

php

Does PHP have a function that searches for hex codes in a string and converts them into their char equivalents?

For example - I have a string that contains the following

"Hello World\x19s"

And I want to convert it to

"Hello World's"

Thanks in advance.

like image 221
Chip Avatar asked Dec 13 '22 17:12

Chip


1 Answers

This code will convert "Hello World\x27s" into "Hello World's". It will convert "\x19" into the "end of medium" character, since that's what 0x19 represents in ASCII.

$str = preg_replace('/\\\\x([0-9a-f]{2})/e', 'chr(hexdec($1))', $str);
like image 169
cdhowie Avatar answered Jan 06 '23 14:01

cdhowie