Horde_Ldap_Util::hex2asc PHP Method

hex2asc() public static method

Converts all hexadecimal expressions ("\HEX") to their original ASCII characters.
Author: [email protected], heavily based on work from [email protected]
public static hex2asc ( string $string ) : string
$string string String to convert.
return string ASCII representation of $string.
    public static function hex2asc($string)
    {
        return preg_replace_callback('/\\\\([0-9A-Fa-f]{2})/', function ($hex) {
            return chr(hexdec($hex[1]));
        }, $string);
    }

Usage Example

Example #1
0
 /**
  * Test HEX unescaping
  */
 public function testHex2asc()
 {
     $expected = '';
     for ($i = 0; $i < 127; $i++) {
         $expected .= chr($i);
     }
     $str = '\\00\\01\\02\\03\\04\\05\\06\\07\\08\\09\\0a\\0b\\0c\\0d\\0e\\0f\\10\\11\\12\\13\\14\\15\\16\\17\\18\\19\\1a\\1b\\1c\\1d\\1e\\1f !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~';
     $this->assertEquals($expected, Horde_Ldap_Util::hex2asc($str));
 }