What is the function in PHP that can be used to count the occurrences of a specific substring in a string?
To count the occurrences of a specific substring in a string in PHP, you can use the `substr_count()` function. This function takes two parameters: the string to search in and the substring to count occurrences of. It returns the number of times the substring appears in the string.
$string = "hello world hello";
$substring = "hello";
$count = substr_count($string, $substring);
echo "The substring '$substring' appears $count times in the string.";
Keywords
Related Questions
- How can error handling be improved in PHP when executing multiple SQL queries to identify issues like empty values being inserted into the database?
- What are the potential pitfalls of storing HTML tags in a PHP forum database for code highlighting?
- Are there any security considerations to keep in mind when implementing an IRC chat on a website with PHP?