php還原16進制特殊字符


特殊字符的16進制表:https://websitebuilders.com/tools/html-codes/ascii/

 

可以通過 hexdec() chr()方法來進行轉換,

例子:

<?xml version=\x221.0\x22 encoding=\x22utf-8\x22?>\x0A<order id=\x22test\x22 affiliate=\x22main\x22 event_id=\x221\x22 ref=\x22TPCYA\x22 alias=\x22\x22/>
 

其中 \x22 就是雙引號 ",而 \x0A 就是換號 \n,通過一些方法轉換后:

    $str = "<?xml version=\x221.0\x22 encoding=\x22utf-8\x22?>\x0A<order id=\x22test\x22 affiliate=\x22main\x22 event_id=\x221\x22 ref=\x22TPCYA\x22 alias=\x22\x22/>";

    function hexdec_string($content) {
        preg_replace_callback(
            "(\\\\x([0-9a-f]{2}))i",
            function($matches) {return chr(hexdec($matches[1]));},
            // $string
            $content
        );  

        return $content;
    }

    echo htmlspecialchars(hexdec_string($str), ENT_QUOTES); //將 xml的內容作為純文本輸出

 

結果:

<?xml version="1.0" encoding="utf-8"?> <order id="test" affiliate="main" event_id="1" ref="TPCYA" alias=""/>

 

參考:https://stackoverflow.com/questions/12238657/decoding-javascript-escape-sequences-in-php-x27-x22-etc
           http://php.net/manual/en/function.preg-replace-callback.php


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM