PHP strcspn() Function

Example

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

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

Run Instance

Definition and Usage

The strcspn() function returns the number of characters found before any specified character 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. Specify the string to be searched.
char Required. Specify the character to be searched.
start Optional. Specify where to start searching in the string.
length Optional. Specify the length of the string (search how many characters).

Technical Details

Return Value: Returns the number of characters found in the string before finding the specified symbol.
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 Instance