php 如何仅使用循环和分解方法输出重复字符串

r8uurelv  于 2022-10-30  发布在  PHP
关注(0)|答案(1)|浏览(155)

例如:“我想我还有很长的路要走。”
在本例中,我希望输出

I 2
think 1
got 1
a 1
long 1
way 1
to 2
go 1
market 1

编码:

<?php
$tmp = explode (" ", $text);
    echo "

        My Words
        Repetition
        ";

         foreach ($tmp as $a)
        {

            echo "" $a."" ;
            echo ""  ;
        }       
        echo ""
?>
gfttwv5a

gfttwv5a1#

您可以尝试使用

$text = "I think I got a long way to go to market.";
$tmp = explode (" ", $text);
foreach($tmp as $a){
   $result[$a] += 1;
}
foreach($result as $value => $occurrences){
   echo "$value $occurrences\n";
}

相关问题