1

I need to count the number of occurences of the substring in a string.

my code like,

$str1 = "when he arrives, he want to open the door.";
$find_str = "he";

here the string "he" is occured 4 times.

how can i find it in php?..

2
  • I have tried by using preg_match_all(); but its not worked fine.. Commented Jun 28, 2013 at 9:56
  • 1
    see the answer, but please: Next time please google this first. It is really not hard to find. Commented Jun 28, 2013 at 9:57

3 Answers 3

4

Use substr_count:

substr_count($str1, $find_str)
Sign up to request clarification or add additional context in comments.

Comments

3

Make use of the substr_count php function.

Solution:

$str1 = "when he arrives, he want to open the door.";
$find_str = "he";

substr_count($str1, $find_str);

Comments

1
echo substr_count($str1, $find_str);

Comments

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.