substr_count

int substr_count(string string, string substring) 

Counts the occurrences of a string within another string.

Returns:

Integer

Description:

substr_count() counts the number of times that substring occurs within string . If substring doesn’t occur within string, the function returns 0.

Version:

PHP 4+ since 4.0RC2

See also:

To get detailed statistics on the characters used in a string:

count_chars() 

Example:

Simple script to test for “Canadian-ness” in a string
 <?php // Don't get yer long-undies in a knot - the author is a Canuck! :) // Set a default value for message $message = ''; // If a message was posted, clean it up if (isset ($HTTP_POST_VARS['message'])) $message = striptags (stripslashes (trim ($HTTP_POST_VARS['message']))); ...

Get PHP Functions Essential Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.