Hello Friends Today, through this tutorial, I will tell you How do you find the number of occurrences of a substring in a string in PHP With Example? You can find the number of occurrences of a substring within a string in PHP using the `substr_count()` function. Here’s an example:
<?php $string = "Hello, hello, hello, how are you?"; $substring = "hello"; $count = substr_count($string, $substring); echo "The number of occurrences of '$substring' in the string is: $count"; ?>
Output:
The number of occurrences of 'hello' in the string is: 3
In this example, the `substr_count()` function takes two arguments: the string you want to search within (`$string`) and the substring you want to count occurrences of (`$substring`). It returns the number of times the substring appears within the string.