PHP: Count the occurrences of a substring in a string

Updated: January 10, 2024 By: Guest Contributor Post a comment

Introduction

Working with strings is a fundamental part of programming, and sometimes we need to quantify instances of specific patterns or sequences within these strings. In PHP, several functions allow us to count occurrences of a substring. This tutorial will guide you through the process of counting substrings within a string using PHP with multiple levels of examples, from basic to advanced techniques.

Understanding substr_count()

The primary function we use in PHP to count the number of times a substring appears within a string is substr_count(). The function is straightforward and comes with options to specify where in the string to start and end the search.

<?php
$string = 'the quick brown fox jumps over the lazy dog';
$substring = 'the';
$count = substr_count($string, $substring);
echo $count; // Outputs: 2
?>

Changing the Search Range

You can specify the search range by providing two additional parameters: the start position and the length of the search:

<?php
$string = 'the quick brown fox jumps over the lazy dog';
$substring = 'the';
$start = 10;
$length = 15;
$count = substr_count($string, $substring, $start, $length);
echo $count; // Outputs: 0
?>

Case-insensitive Searches

To perform a case-insensitive search, we combine substr_count() with strtolower(), since substr_count() is case-sensitive by default:

<?php
$string = 'The quick brown fox jumps over the lazy dog';
$substring = 'the';
$count = substr_count(strtolower($string), strtolower($substring));
echo $count; // Outputs: 2
?>

Advanced Usage: Regex Patterns with preg_match_all()

For more sophisticated pattern matching that can’t be handled by substr_count(), PHP’s preg_match_all() function comes into play. It allows for regular expression pattern searches:

<?php
$string = 'The1 quick2 brown3 fox4 jumps5 over6 the7 lazy8 dog9';
$pattern = '/\d+/';
$count = preg_match_all($pattern, $string, $matches);
echo $count; // Outputs: 9
print_r($matches); // Outputs the array of matches
?>

Debugging Tips

It’s not uncommon for substring counting to produce unexpected results. When this happens, ensure your inputs are correctly formatted and consider if special characters may be affecting the search.

Performance Considerations

When working with very large strings or complex regular expressions, performance can be an issue. Monitor the execution time and memory usage, adjusting your algorithm or regex pattern as necessary to optimize the performance.

Conclusion

PHP offers robust string manipulation functions suitable for a variety of tasks, including counting substring occurrences. Whether you’re performing a basic count with substr_count() or delving into regex patterns with preg_match_all(), PHP’s flexible options cater to both new and experienced developers alike. With practice and the right approach, these functions can be powerful tools for your development projects.