PHP strcspn() Function
Example
Output the number of characters found before the character "w" in the string "Hello world!":
Run Example »
Definition and Usage
The strcspn() function returns the number of characters found in the string before any of the specified characters is found (including spaces).
Tip: Use the strspn() function to the number of characters found in the string that contains only characters from a specified character list.
Note: This function is binary-safe.
Syntax
strcspn(
string,char,start,length
)
| Parameters | Description |
|---|---|
| string | Required. Specifies the string to search. |
| char | Required. Specifies the characters to find. |
| start | Optional. Specifies the position to start searching. |
| length | Optional. Specifies the length of the string (how many characters to search). |
Technical Details
| Return Value: | Returns the number of characters found in the string before any of the specified characters is found. |
|---|---|
| PHP Version: | 4+ |
| Changelog: | In PHP 4.3, the start and length parameter was added. |
More examples
Example 1
Use all parameters to output the number of characters found before the character "w" in the string "Hello world!":
Run Example »
PHP String Reference Manual