Converting named HTML entities to numeric HTML entities

前端 未结 6 1856
半阙折子戏
半阙折子戏 2020-12-08 08:18

Is there a PHP function to convert named HTML entities into their respective numeric HTML entities?

For example:

$str = \"Oggi è un bel&am         


        
6条回答
  •  -上瘾入骨i
    2020-12-08 08:30

    This one

    1. does not require enumeration of entities in user's code,
    2. works on HTML code containing named entities (rude applying html_entity_decode to the whole string messes up < and > (converted < and >) and HTML tag start/end):

    Here it is

    function htmlent2xml($s) {
        return preg_replace_callback("/(&[a-zA-Z][a-zA-Z0-9]*;)/",function($m){
           $c = html_entity_decode($m[0],ENT_HTML5,"UTF-8");
           # return htmlentities($c,ENT_XML1,"UTF-8"); -- see update below
    
           $convmap = array(0x80, 0xffff, 0, 0xffff);
           return mb_encode_numericentity($c, $convmap, 'UTF-8');
        },$s);
    }
    

    upd: Following Carlos's remark, the proposed solution is fixed using this answer to get the required numeric entities.

提交回复
热议问题