douhu2131 2012-09-08 20:28
浏览 44
已采纳

PHP在字符串中检测和拆分html特殊字符代码?

In PHP when i read the Data, lets say the data (chunk of string) is containing HTML Special Character DECIMAL HEX Codes like:
This is a sample string with < œ < and š

What i want is, how to Detect and Split out the Decimal Hex Codes (of any Special Characters) inside a chunk of string?

For example, above string contains:

  • Two Count of <
  • One Count of œ
  • One Count of š

How can i programatically detect it (The OCCURRENCE for any Html Special Characters)?
(Collected results will be better as an Array)

  • 写回答

4条回答 默认 最新

  • douzhigan1687 2012-09-08 20:41
    关注

    I think this is what you are after:

    $s = 'This is a sample string with œ and š';
    
    $pattern = '/\&#x\d+\;/';
    
    preg_match_all($pattern, $s, $matches);   
    
    var_dump( $matches );
    

    This will output:

    array(1) {
      [0]=>
      array(2) {
        [0]=>
        string(7) "œ"
        [1]=>
        string(7) "š"
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?