What are the advantages and disadvantages of using substr_count compared to regular expressions for counting occurrences in PHP?
When counting occurrences in PHP, substr_count is often faster and more efficient than using regular expressions. Substr_count simply counts the number of occurrences of a substring within a string, while regular expressions provide more flexibility but can be slower for simple counting tasks. However, regular expressions are more powerful and versatile for complex pattern matching.
// Using substr_count to count occurrences of a substring
$string = "hello world hello";
$substring = "hello";
$count = substr_count($string, $substring);
echo $count; // Output: 2