PHP strcspn() Function

Example

Output the number of characters found before the character "w" is found in the string "Hello world!"

<?php
echo strcspn("Hello world!","w");
?>

Run Examples

Definition and Usage

The strcspn() function returns the number of characters found before any specified character is found in the string search (including spaces).

Tip:Please use strspn() The function returns the number of characters in the string that contain characters from the specified character list.

Note:This function is binary safe.

Syntax

strcspn(string,char,start,length)
Parameters Description
string Required. Specifies the string to be searched.
char Required. Specifies the character to be searched for.
start Optional. Specifies where to start searching in the string.
length Optional. Specifies the length of the string (search how many characters).

Technical Details

Return Value: Returns the number of characters found in the string before the specified character is found.
PHP Version: 4+
Update Log: In PHP 4.3, a new feature was added: start and length Parameters.

More Examples

Example 1

Use all parameters to output the number of characters found before the character "w" in the string "Hello world!"

<?php
echo strcspn("Hello world!","w",0,6); // The starting position is 0, the length of the search string is 6.
?>

Run Examples