Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,954 questions

51,896 answers

573 users

How to count substring occurrences in a larger string in PHP

1 Answer

0 votes
echo substr_count("isset email session email empty email", "email") . "\n"; // 3
echo substr_count("themes email styleemail", "email") . "\n"; // 2
echo substr_count("emailemail abcdeeeemaillll", "email") . "\n"; // 3

// from char 13 'session email empty email'
echo substr_count("isset email session email empty email", "email", 13) . "\n"; // 2

// from char 13 to 20 'session email empty'
echo substr_count("isset email session email empty email", "email", 13, 20) . "\n"; // 1

// substr_count doesn't count overlapped substrings
echo substr_count("abcabcabc", "abcabc") . "\n"; // 1



/*
run:

3
2
3
2
1
1

*/


answered May 23, 2014 by avibootz
edited Feb 27, 2025 by avibootz
...