Php strcspn
From w3cyberlearnings
Contents |
PHP function strcspn
This function returns the number of characters found before the search key.
Syntax strcspn
- string: string to input.
- char: search character key.
- start: specifies where in string to start
- length: width of the string to search
strcspn(string,char,start,length)
Example 1
<?php //12 $c1 = strcspn('gok', 'k'); //2 //123 $c2 = strcspn('gook', 'k'); //3 //1234 $c3 = strcspn('goook', 'k'); //4 //12345 $c4 = strcspn('gooook', 'k'); //5 //123456 $c5 = strcspn('goooook', 'k'); //6 echo '$c1: ' . $c1 . '<br/>'; echo '$c2: ' . $c2 . '<br/>'; echo '$c3: ' . $c3 . '<br/>'; echo '$c4: ' . $c4 . '<br/>'; echo '$c5: ' . $c5 . '<br/>'; ?>
Output
$c1: 2 $c2: 3 $c3: 4 $c4: 5 $c5: 6
Example 2
<?php echo strcspn('excellent', 'l', 2, 2); ?>
Output
2
Example 3
<?php //12 echo strcspn('a0e', 'e', 2) . '<br/>'; //12x echo strcspn('a00e', 'e', 2) . '<br/>'; //12xx echo strcspn('a000e', 'e', 2) . '<br/>'; //12xxx echo strcspn('a0000e', 'e', 2) . '<br/>'; ?>
Output
0 1 2 3
Example 4: Search start at location 2, and the lenght of the search string
<?php //12 echo strcspn('a0e', 'e', 2,2) . '<br/>'; //12x echo strcspn('a00e', 'e', 2,2) . '<br/>'; //12xx echo strcspn('a000e', 'e', 2,2) . '<br/>'; //12xxx echo strcspn('a0000e', 'e', 2,2) . '<br/>'; ?>
Output
0 1 2 2