不要在阅读时间和 word/character 计数器中包括 bbCode

Don't include bbCode in reading time and word/character counters

我在 PHP 中使用不同的功能来帮助我计算单词、字符以及阅读时间。但它们都有一点 "error":函数计算一切 - 包括 bbCode(带笑脸)。我不要那个!

function calculate_readingtime($string) {
    $word = str_word_count(strip_tags($string));
    $m = floor($word / 200);
    $s = floor($word % 200 / (200 / 60));

    $minutes = ($m != 0 ? $m.' min.' : '');
    $seconds = (($m != 0 AND $s != 0) ? ' ' : '') . $s.' sec.';

    return $minutes . $seconds;
}

$content = 'This is some text with [b]bbCode[/b]! Oh, so pretty :D And here\'s is a link too: [url="https://example.com/"]das linkish[/url]. What about an image? That\'s pretty to, you know. [img src="https://example.com/image.jpg" size="128" height="128" width="128"] And another one: [img src="https://example.com/image.jpg" height="128"]';
$reading_time = calculate_readingtime($content);
$count_words = str_word_count($content, 1, 'àáãâçêéíîóõôúÀÁÃÂÇÊÉÍÎÓÕÔÚÅåÄäÖö');
$count_chars_with_spaces = mb_strlen($content);

echo 'Reading time: '.$reading_time.'<br>';
echo 'Words: '.count($count_words).'<br>';
echo 'Characters with spaces: '.$count_chars_with_spaces;

# OUTPUT
Reading time: 16 sec.
Words: 55
Characters with spaces: 326

我希望计数器(包括阅读时间)更准确并且不包括 bbCode 但包括 bbCode 中的文本(例如:包括来自 [=13 的文本 bbCode =]).

我怎样才能做到这一点?

使用 preg_replace 从字符串中解析 BBCode 实际上相对容易,尤其是在 PHP 等支持 PCRE 库的语言中。假设关于您的 BBCode 语法的一些事情,这是最短的方法:

preg_replace('@\[(?:\w+(?:="(?>.*?"))?(?: \w+="(?>.*?"))*|/\w+)]@s', '', $content);

Demo on Regex101

或者使用结束标记和嵌套更精确的更好方法:

function parse($str) {
    return preg_replace_callback('@\[(\w+)(?:="(?>.*?"))?(?: \w+="(?>.*?"))*](?:(.*?)\[/])?@s',
        function($matches) { return $matches[2] ? parse($matches[2]) : ''; },
        $str
    );
}

Demo on Ideone