PHP substr_count() Function

PHP substr_count() Function

The substr_count() function in PHP counts the number of times a substring occurs in the specified string.

Syntax

substr_count ( string $haystack , string $needle [, int $offset  [, int $length ]] )

Parameter

haystack(required)- This parameter signifies the string to search in.

needle (required)- This parameter represents the substring to search for.

offset (optional)- This parameter represents the offset where to start counting, and the default value is 0. If it is negative, counting starts from the end of the string.

length (optional)- It signifies the length of the search after the specified offset to search for the substring. If it is negative, the length counts from the end of haystack. If the sum of offset and length is greater than the haystack length, it outputs a warning.

Return

This function returns the number of times the substring occurs in the string.

Example 1

 

Output

String 1: Hello PHP! This is my PHP example.
String 2: PHP
After the substr_count() function...
The returned value: 2 

Example 2

 

Output

String 1: Hello PHP! This is my PHP example.
String 2: PHP
After the substr_count() function...
The returned value: 2 

Example 3

 

Output

String 1: Hello PHP! This is my PHP example.

After the substr_count() function...
The returned value: 0

Example 4

 33
 $count = substr_count($str1, 'is', 33, 3);
 echo "\nThe returned value: ".$count; // 
 ?> 

Output

PHP Warning:  substr_count(): Invalid length value in /workspace/Main.php on line 8

Related Topics

PHP array_key_last() function

The array_key_last () function in PHP gets the last key of an array if the specified array is not empty. Syntax array_key_last ( array $array )  Parameter array(required)- This parameter signifies the input array. Return This function returns the last key of the...

1 minute read.

PHP Constant

PHP constant is an identifier(name) for a simple value. The value does not change during the script. It starts with a letter or underscore not $ sign. The define()function is...

1 minute read.

PHP soundex() Function

PHP soundex() Function The soundex() function in PHP Calculates the soundex key(four character long alphanumeric string that represent English pronunciation of a word) of a string. Syntax soundex ( string $str ) Parameter str(required) – This parameter represents the input string. Return This function returns the soundex...

1 minute read.

PHP boolval() Function

PHP boolval() Function The boolval() function in PHP is used to give the Boolean value for a given variable. Syntax boolval ( mixed $var ) Parameter var(required)- This parameter represents the value that is converted to a boolean. It can be a...

1 minute read.

PHP strval() function

PHP strval() function The strval() function in PHP is used to get the string value for the specified variable. This function cannot be used on arrays or objects, if applied then would return only the...

1 minute read.

PHP strspn() Function

PHP strspn() Function The strspn() function of PHP finds the length of the initial segment of string that contains only characters from the mask parameter. Syntax strspn(string$subject,string$mask[,int$start[,int$length]] ) Parameter subject(required)- This parameter represents the input string. mask(required)- This parameter specifies the characters to find. start (optional)- It signifies the position in the string to start...

1 minute read.

PHP is_long() Function

The PHP is_long () function in PHP is used to find whether a variable is an integer or not. It returns a Boolean value true if the parameter var is integer else it returns...

1 minute read.

PHP Associative Arrays

PHP associate array is used to access the elements with keys by “=>” symbol. There are two ways to create an associative array. 1st way: $age = array("John"=>"20", "Helena"=>"30", "Rasmus"=>"29"); 2nd way: $age['john'] = "20"; $age['Helena'] =...

1 minute read.

PHP floatval() Function

PHP floatval() Function The floatval() function in PHP is used to give the float value of a variable. Syntax floatval ( mixed $var ) Parameter var(required)- This parameter represents the name of the variable that is to be converted. Return This function...

1 minute read.

PHP strrrev() Function

PHP strrrev () Function The strrev() function in PHP reverses a string. Syntax strrev ( string $string ) Parameter string(required)- This parameter represents the input string to be reversed. Return This function returns the reversed string. Example 1 Output Initial string: Hello...

1 minute read.

PHP array_values() Function

PHP array_values() Function The array_values () function in PHP returns all the values from the array and indexes the array numerically. Syntax array_values ( array $array ) Parameter array(required)- This array represents the input array. Return This function returns an indexed array of values. Example 1 Output Input Array: Array ...

1 minute read.

PHP reset() Function

PHP reset() Function The reset() function in PHP is used to set the internal pointer of an array to its first element and returns the value of the first array element. Syntax reset ( array &$array )  Parameter array(required)- This parameter represents...

1 minute read.

PHP str_rot13() Function

PHP str_rot13() Function The str_rot13() function in PHP performs the ROT13 encoding on a string and returns the resulting string. The ROT13 encoding is used to shift every letter by 13 places in the alphabet...

1 minute read.

PHP array_intersect_assoc() function

PHP array_intersect_assoc() function The array_intersect_assoc() function in PHP is used to compute the intersection of arrays with the help of an additional index check. It returns an array containing all the entries from array1 that are present...

1 minute read.

WordPress Plugin

WordPress Plugin is a process used to add or extend the functionality which can be enabled or disabled, and it does not interfere with the software and its code. WordPress is lightweight and smooth...

2 minutes read.

PHP str_word_count() Function

PHP str_word_count() Function The str_word_count() function in PHP counts the number of words inside the string and returns the information about words used in a string. String str_word_count( string $string [, int $format [, string $charlist ]] ) Parameter string(required)- This parameter represents the input string. format(required)- This parameterspecifies the...

2 minutes read.

PHP ord() Function

PHP ord() Function The ord() function in PHP converts the first byte of a string to a value between 0 and 255. This function interprets the binary value of the first byte of string as an...

1 minute read.

PHP wordwrap() Function

PHP wordwrap() Function The wordwrap() Function wraps a string to a given number of characters using a string break character. Syntax wordwrap ( string $str [, int $width [, string $break  [, bool $cut  ]]] ) Parameter str(required)- This parameter specifies the input string.       width(optional)- This parameter represents the number of...

1 minute read.

PHP var_dump() Function

PHP var_dump() Function The var_dump() function in PHP is used to dump the information about a variable. It displays the structured information (arrays and objects) about one or more expressions. This function is also effective...

1 minute read.

PHP get_defined_vars() Function

PHP get_defined_vars() Function The get_defined_vars() function in PHP returns an array of all defined variables. It returns a multidimensional array containing a list of all defined variables. Syntax get_defined_vars ( void ) Parameter NA Return This function returns a multidimensional array containing a list...

1 minute read.