PHP: substr_count() function
Description
The substr_count() function is used to count the number of times a substring occurs in a string. Note that substring is case sensitive.
Version:
(PHP 4 and above)
Syntax:
substr_count(string1, string2, nstart, nlength)
Parameters:
Name | Description | Required / Optional |
Type |
---|---|---|---|
string1 | The string to search in. | Required | String |
string2 | The substring to search for. | Required | String |
nstart | The position where string to start searching. | Optional | Integer |
nlength | The length of the string to be searched for. | Optional | Integer |
Return value:
Returns a number.
Value Type: Integer.
Pictorial Presentation
Example:
<?php
$string1="Welcome to w3resource.com";
echo substr_count($string1,'co');
echo substr_count($string1,'co',4);
echo substr_count($string1,'co',4,4);
?>
Output:
2 1 0
View the example in the browser
See also
Previous: substr_compare
Next: substr_replace
PHP: Tips of the Day
PHP: Remove first 4 characters of a string with PHP
You could use the substr function to return a substring starting from the 5th character:
$str = "The quick brown fox jumps over the lazy dog." $str2 = substr($str, 4); // "quick brown fox jumps over the lazy dog."
Ref : https://bit.ly/2LJDLQx
- New Content published on w3resource:
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- React - JavaScript Library
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework