PHP strcspn()Function

PHP String 参考手册PHP String Reference Manual

Examples

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

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

Run example »

Definition and Usage

The strcspn() function returns the number of characters found in a string before any of the specified characters are 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 the search.
length Optional. Specifies the length of the string (how many characters to search).

Technical Details

Return value: Returns the number of characters found in a string before any of the specified characters are found.
PHP Version: 4+
Changelog: In PHP 4.3, the following was added:startandlengthparameter.


More Examples

Example 1

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

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

Run example »


PHP String 参考手册PHP String Reference Manual Other Extensions