用 PHP 中的 HTML 编号实体计算字符串的长度

Squ*_*ler 6 php strlen html-entities

我想用 PHP 计算字符串的长度。该字符串包含 HTML 实体编号,这会增加计算的字符数:–当我只想将其计为 1 时,破折号计为 7。

如何将 html 编号的实体转换为特殊字符仅计算长度为 1 的形式?

示例字符串:

Goth-Trad – ‘Cosmos’
Run Code Online (Sandbox Code Playgroud)

编码:

$string = html_entity_decode('Goth-Trad – ‘Cosmos’');
    echo strlen($string);
Run Code Online (Sandbox Code Playgroud)

生成 '38',当我在寻找 '20' 时。出了什么问题?

Pet*_*jci 5

你可以使用这个:

$html = 'Goth-Trad – ‘Cosmos’';
echo strlen(utf8_decode(html_entity_decode($html, ENT_COMPAT, 'utf-8')));
Run Code Online (Sandbox Code Playgroud)