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 Example

Definition and Usage

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

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

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 symbol is found.
PHP Version: 4+
Update Log: In PHP 4.3, a new 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 Example