0

I need to count some specific words in string(How many times some word occurs in a string), How can I do it in php?

Example;

$words = array("woman", "murder", "rape", "female", "dowry");

$string = "A dowry deaths is a murder or suicide of a married woman caused by a dispute over her dowry.[4] In some cases, husbands and in-laws will attempt to extort a greater dowry through continuous harassment and torture which sometimes results in the wife committing suicide";

The first item is the bundle of some word, If this word match with below string increase its value....

Example of result like be:

dowry= 1;
murder =2;
women =5;
1

4 Answers 4

3

Try:

substr_count — Count the number of substring occurrences

$string = "A dowry deaths is a murder or suicide of a married woman caused by a dispute over her dowry.[4] In some cases, husbands and in-laws will attempt to extort a greater dowry through continuous harassment and torture which sometimes results in the wife committing suicide";

$words = array("women", "murder","rape","female","dowry");

foreach($words as $word) {
   echo $word." occurance are ".substr_count($string, $word)." times <br />";
}

Output:

women occurance are 0 times

murder occurance are 1 times

rape occurance are 0 times

female occurance are 0 times

dowry occurance are 3 times

Sign up to request clarification or add additional context in comments.

1 Comment

If the given string has a word say 'deathswomen' then it will return women occurance are 1 times which I think is not correct if you want to count only complete words and not just substring.
2

function substr_count does what you want

foreach($words as $word)
  echo $word .' ' . substr_count($string, $word) . "\n";

result

women 0
murder 1
rape 0
female 0
dowry 3

demo

Comments

1

using substr_count is going to be straightforward

$string = 'I just ate a delicious apple, and John ate instead a banana ice cream';
$words = ['banana', 'apple', 'kiwi'];
foreach($words as $word) {
    $hashmap[$word] = substr_count($string, $word);
}

now $hashmap contains an associative array with word => occurrences

array(3) {
  ["banana"] => int(1)
  ["apple"]  => int(1)
  ["kiwi"]   => int(0)
}    

Comments

0

You can Split the string into words and then loop through the array of words and check if it is one of the words inside the $words array is the same.

$arrayOfString = explode(" ", $string); //splitting in between spaces will return a new array
for ($i = 1; $i <= $arrayOfString.count(); $i++) //loop through the arrayOfString array
{
    for($j = $words.count(); $j >= 1; $j--)//loop through the words array
    {
         if($arrayOfString[i] == $words[j])
         {
              echo("You have a match with: " + $words[j]); //Add your count logic here
         }
    }
}

Hopefully this works for you.

There are probably better ways for you to do this, but I like this logic myself.

1 Comment

this is not java dude

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.