不含空格和html标签的Php字符计数


Php character count without spaces and html tags

我想计算像Microsoft Word这样的文本中的字符数。

<p><b>Lorem Ipsum</b> is simply dummy text of the printing and typesetting industry.Tom's farm. 12th century.</p>

12 -> 'th'是数字12下面的一个特殊字符。

如何在PHP中做到这一点?

我的解决方案是;

    $content = 'Some Text...';
    $trim = strip_tags($content);
    $trim=str_replace([" ","'n","'t","&ndash;","&rsquo;","&#39;","&quot;","&nbsp;"], '', $trim);
    $totalCharacter = strlen(utf8_decode($trim));
$string = "<p><b>Lorem Ipsum</b> is simply dummy text of the printing and typesetting industry.Tom's farm. 12th century.</p>";
$string = strip_tags($string);
$string = preg_replace("/'s/", "", $string);
$character_count = strlen($string);

如果有第一个Unicode字符,strlen可能会把它算作两个字符。然后你应该看看mb_strlen。