【问题标题】:html_entity_decode() isn't work (PHP)html_entity_decode() 不起作用 (PHP)
【发布时间】:2014-12-13 12:22:41
【问题描述】:

html_entity_decode() 返回 HTML 实体,而不是适用的字符。

<meta http-equiv="Content-Type" content="text/html charset=UTF-8">
<?php

$code='&#97'; // It's a code of 'a' UTF-8 character.

$char = html_entity_decode($code, ENT_COMPAT, $encoding = 'UTF-8');

/*
Now $char must contains 'a' value, but it contains '&#97';

You can check this by following tests
*/

var_dump('&#97' === 'a');      // bool(false), of course.
var_dump('&#97' === $code);    // bool(true)
var_dump('&#97' === $char);    // bool(true) BUT MUST BE FALSE
var_dump($code === $char);     // bool(true) BUT MUST BE FALSE

// Or this:

echo str_replace('&', '', $char);    // it must print 'a', but it print '#97'

看起来这个函数在我的情况下什么都不做。 怎么了?

【问题讨论】:

  • 您的字符引用缺少分隔符;:&amp;#97;
  • PHP 不是 Python,它没有命名参数。 $encoding = 'UTF-8' 可能不会像您认为的那样做。函数调用应该是简单的html_entity_decode($code, ENT_COMPAT, 'UTF-8')

标签: php utf-8


【解决方案1】:

&amp;#97 不是“'a' UTF-8 字符的代码。”。

但是,&amp;#97; 是。使用它,您的代码将按预期工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-02
    • 2014-01-23
    • 1970-01-01
    • 2011-06-06
    • 2016-02-03
    • 1970-01-01
    • 1970-01-01
    • 2019-02-09
    相关资源
    最近更新 更多