strcspn
Get the length of the starting substring that does not match the mask
strcspn()
function returns the number of characters (including spaces) that are found on the string before any specified character is found.
Tip: Please use the strspn()
function to return the number of characters in the string containing the specified character list.
Note: This function is binary safe.
Output the number of characters found before the character "w" is found in the string "Hello world!":
<?php echo strcspn ( "Hello world!" , "w" ) ; ?>
Try it yourself
Use all parameters to output the number of characters that were found before the character "w" is found in the string "Hello world!":
<?php echo strcspn ( "Hello world!" , "w" , 0 , 6 ) ; // The start position is 0, and the length of the search string is 6. ?>
Try it yourself
strcspn ( string , char , start , length )
parameter | describe |
---|---|
string | Required. Specifies the string to search. |
char | Required. Specifies the characters to be searched. |
start | Optional. Specifies where to start searching in the string. |
length | Optional. Specifies the length of the string (how many characters are searched for). |