Thursday, 1 September 2016

PHP : substr_count() function


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

NameDescriptionRequired /
Optional
Type
string1The string to search in.RequiredString
string2The substring to search for.RequiredString
nstartThe position where string to start searching.OptionalInteger
nlengthThe length of the string to be searched for.OptionalInteger

Return value

Returns a number.
Value Type : Integer.

Example :

  1. <?php  
  2. $string1="Welcome to w3resource.com";  
  3. echo substr_count($string1,'co');   
  4. echo substr_count($string1,'co',4);   
  5. echo substr_count($string1,'co',4,4);   
  6. ?>  

Output

2
1
0

0 comments:

Post a Comment