Home > Software engineering >  what can l change about this function to get the result that l need? PHP
what can l change about this function to get the result that l need? PHP

Time:09-23

what can I change about this function so that I can call the function this way. for example: wordcounter(5,$string) then I should get number less than 5 if I call out wordcounter(3,$string ) the output should be less than 3 letters. or if I call this wordcounter(3,"this is an example") / the output must be words that are less than 3. in this example only 1'.

function Wordcounter($string){
     $stringArray = explode(" ",$string);
     $counter=0;
     foreach ($stringArray as $value){
        if(strlen($value) < 4){
            $counter  ;
        }
    }
    return $counter;
}

$string = "Voor de vormgeving is het handig om te weten hoe het eruit komt te zien voordat je daadwerkelijk tekst gaat plaatsen.";

$string = Wordcounter($string);

echo $string;

CodePudding user response:

you really shouldn't have people do your homework for you. you end up not learning. regardless, this would be pretty easy.

function Wordcounter($char_limit, $string){
     $stringArray = explode(" ",$string);
     $counter=0;
     foreach ($stringArray as $value){
        if(strlen($value) < $char_limit){
            $counter  ;
        }
    }
    return $counter;
}
  • Related