Output the number of characters searched before the character "w" is found in the string "Hello world!":
<?phpecho strcspn("Hello world!","w");?>The strcspn() function returns the number of characters (including spaces) searched in a string before any of the specified characters are found.
Tip: Use the strspn() function to the number of characters found in the string that contains only characters from a specified character list.
Comment: This function is binary-safe.
strcspn( string,char,start,length )
parameter | describe |
---|---|
string | Required. Specifies the string to search for. |
char | Required. Specifies the characters to search for. |
start | Optional. Specifies the location to start the search. |
length | Optional. Specifies the length of the string (how many characters to search for). |
Return value: | Returns the number of characters searched in a string before any specified character is found. |
---|---|
PHP version: | 4+ |
Update log: | In PHP 4.3, new start and length parameters were added. |
Use all arguments to print the number of characters searched until the character "w" is found in the string "Hello world!":
<?phpecho strcspn("Hello world!","w",0,6); // The start position is 0 and the length of the search string is 6.?>