Home > Software design >  How to output repeat strings with looping and explode method only
How to output repeat strings with looping and explode method only

Time:10-27

e.g. "I think I got a long way to go to market."

In this case, I want to output

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

Code:

<?php
$tmp = explode (" ", $text);
    echo "
         
        My Words
        Repetition
        ";
        
         foreach ($tmp as $a)
        {
            
            echo "" $a."" ;
            echo ""  ;
        }       
        echo ""
?>

CodePudding user response:

You can try with

$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";
}
  • Related