Jump to: navigation, search

Php strspn

From w3cyberlearnings

Contents

PHP function strspn

This function returns the length found in the string that contains only characters from the charlist.

Syntax strspn

  • string: spring to search
  • charlist: list of characters to search
  • start: where to start
  • length: defines the length of the string to be return.
strspn(string,charlist,start,length)

Example 1

  • Return the match of the first segment of the string (i.e "ood", and ignore the rest of the string).
<?php

echo strspn("Good old day","od",1);

?>

Output

3

Example 2

  • The same as the above example, however this time we allow only the return match with 2 in length.
<?php

echo strspn("Good old day","od",1,2);

?>

Output

2

Example 3

<?php
// start at 1, and return length is 5
echo strspn("Fooood", "o", 1, 5) . '<br/>'; 

// start is 1, and return length is 4
echo strspn("Fooood", "o", 1, 4) . '<br/>';

// start is 1, and return length is 3
echo strspn("Fooood", "o", 1, 3) . '<br/>';

// start is 1 and return length is 2
echo strspn("Fooood", "o", 1, 2) . '<br/>';
?>

Output

4
4
3
2

Example 4

<?php

echo strspn("Hello OLD day friends","Hel");
?>

Output

Match:Hell

4

Related Functions


Navigation
Web
SQL
MISC
References