2

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

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

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

它显示:

i -
like -
apples -
but - 
sometimes -
i -
eat - 
bananas -
4

3 回答 3

5

没有办法回答这个问题而不听起来像一个聪明的屁股,但是strlen......

于 2013-06-17T12:21:24.380 回答
4
foreach(explode(" ", $_POST['txt']) as $word)
    echo $word." - ".strlen($word)."<br>";
于 2013-06-17T12:22:22.477 回答
2
$words = explode(" ", $_POST['txt']);

for ($i=0; $i<count($words); $i++){ 
    echo "<br/>" . $words[$i] . " - " . strlen($words[$i]);
}
于 2013-06-17T12:23:33.613 回答