我设法获得了在不同行上显示的单词数,并且我需要每个单词旁边由“-”分隔的单词的字符数。我怎样才能做到这一点?

字符串来自一个文本区域:“我喜欢苹果,但有时我吃香蕉”

$words = explode(" ", $_POST['txt']);
    for ($i=0; $i<count($words); $i++){
        echo "<br/>" . $words[$i] . " - ";}

它显示:
i -
like -
apples -
but -
sometimes -
i -
eat -
bananas -

最佳答案

$words = explode(" ", $_POST['txt']);

for ($i=0; $i<count($words); $i++){
    echo "<br/>" . $words[$i] . " - " . strlen($words[$i]);
}

关于php - 计算PHP字符串中每个单词的字符数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17147345/

10-15 17:37