php html_entity_decode实例教程

关于html_entity_decode在大多数情况下是与htmlspecialchars htmlentities配合使用的.

html_entity_decode用法:

string html_entity_decode(string $string [,int $quote_style = ENT_COMPAT [, string $charset ]]).

html_entity_decode() 函数把 HTML 实体转换为字符.

html_entity_decode() 是 htmlentities() 的反函数也html_entity_decode() 是 htmlspecialchars() 的反函数.

  1. $str = "A 'quote' is bold www.phpfensi.com";
  2. Outputs: A 'quote' is <b>bold</b>
  3. $s = htmlspecialchars($str);
  4. echo $s;
  5. Outputs: A 'quote' is <b>boldwww.111cn.net</b>
  6. echo html_entity_decode($s);
  7. A 'quote' is bold
再看一个实例,代码如下:
  1. $str = "John & 'Adams'";
  2. echo html_entity_decode($str);
  3. echo "

    " >;

  4. echo html_entity_decode($str, ENT_QUOTES);
  5. echo "

    " >;

  6. echo html_entity_decode($str, ENT_NOQUOTES);
  7. ?>//开源代码phpfensi.com
  8. //浏览器输出:
  9. John & 'Adams'
  10. John & 'Adams'
  11. John & 'Adams'

如果在浏览器中查看源代码,会看到这些 HTML:

  1. <html>
  2. <body>
  3. John & 'Adams'<br />
  4. John & 'Adams'<br />
  5. John & 'Adams'
  6. body>
  7. html>