PHP substr count

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

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

string_substr_count

Example :

  1. $string1="Welcome to w3resource.com";  

  2. echo substr_count($string1,'co');   

  3. echo substr_count($string1,'co',4);   

  4. echo substr_count($string1,'co',4,4);   

  5. ?>  

Output :

2
1
0