Home >>PHP String Functions >PHP strcspn() Function
PHP strcspn() Function
PHP strcspn() function is used to get the number of characters present in a given input string before any part of the defined characters to be searched are found. It is case-sensitive and binary-safe.
Syntax:
strcspn( $string, $char, $start, $length);
Parameter | Description |
---|---|
string | This is a required parameter. This parameter defines the string to search. |
char | This is a required parameter. This parameter defines the characters to search for. |
start | This is an optional parameter. This parameter defines where in string to start from. |
length | This is an optional parameter. This parameter defines the length of the string. |
Here is an example of strcspn() function in PHP:
<html> <body> <?php echo strcspn("PHPTPOINT","O"); ?> </body> </html>
Here is an another example of strcspn() function in PHP
<html> <body> <?php $str = "Hello Everyone.. I am Jerry"; echo strcspn($str,"J",0,25)."<br>";// start position is 0 and length of the search string is 25. echo strcspn($str,"J",5,20)."<br>"; // start position is 5 and length of the search string is 20. echo strcspn($str,"J",15,10)."<br>"; // start position is 5 and length of the search string is 20. echo strcspn($str,"J",8,20)."<br>"; // start position is 5 and length of the search string is 20. ?> </body> </html>