htmlentities 和 html_entity_decode 的行为并不像反向
htmlentities and html_entity_decode do not behave as reverse
我想将字符串截断为一定数量的字符。此字符串包含 html 个字符。请注意,我从字符串中删除了所有 html 标签。现在,如果断点处有一个特殊字符,它不应该在 html 字符的中间中断,而是在之前或之后。这些示例不起作用:
//example 1
$str = "French for French is français";
$str = substr($str, 0, 27);
//$str contains "French for French is fran&c";
//example 2
$str = "the en dash looks like –";
$str = substr($str, 0, 25);
//$str contains "the en dash looks like &#";
所以我想我应该先将特殊字符转换为单个字符,进行截断,然后将单个字符还原为特殊字符。它似乎在第一个示例中有效,但在第二个示例中无效。
//example 1
$str = "French for French is français";
$str = html_entity_decode($str);
$str = substr($str, 0, 27);
$str = htmlentities($str);
//$str contains "French for French is frança";
//example 2
$str = "the en dash looks like –";
$str = html_entity_decode($str);
$str = substr($str, 0, 25);
$str = htmlentities($str);
//$str contains "the en dash looks like &#";
我应该更改哪些内容才能使这两个示例的行为符合我的预期?
htmlentities 默认使用您的 default_charset
php.ini 编码值。如果您没有使用支持您正在转换的实体的字符集,它可能不会按预期运行。试试这个,看看是否会得到不同的结果。
htmlentities($str, null, 'utf-8');
html_entity_decode($str, null, 'utf-8');
mb_substr($str, 0, 25, 'utf-8');
我想将字符串截断为一定数量的字符。此字符串包含 html 个字符。请注意,我从字符串中删除了所有 html 标签。现在,如果断点处有一个特殊字符,它不应该在 html 字符的中间中断,而是在之前或之后。这些示例不起作用:
//example 1
$str = "French for French is français";
$str = substr($str, 0, 27);
//$str contains "French for French is fran&c";
//example 2
$str = "the en dash looks like –";
$str = substr($str, 0, 25);
//$str contains "the en dash looks like &#";
所以我想我应该先将特殊字符转换为单个字符,进行截断,然后将单个字符还原为特殊字符。它似乎在第一个示例中有效,但在第二个示例中无效。
//example 1
$str = "French for French is français";
$str = html_entity_decode($str);
$str = substr($str, 0, 27);
$str = htmlentities($str);
//$str contains "French for French is frança";
//example 2
$str = "the en dash looks like –";
$str = html_entity_decode($str);
$str = substr($str, 0, 25);
$str = htmlentities($str);
//$str contains "the en dash looks like &#";
我应该更改哪些内容才能使这两个示例的行为符合我的预期?
htmlentities 默认使用您的 default_charset
php.ini 编码值。如果您没有使用支持您正在转换的实体的字符集,它可能不会按预期运行。试试这个,看看是否会得到不同的结果。
htmlentities($str, null, 'utf-8');
html_entity_decode($str, null, 'utf-8');
mb_substr($str, 0, 25, 'utf-8');