PHP strspn() Function
Topic: PHP String ReferencePrev|Next
strspn() function returns the number of characters present in the initial part of the string that contains only characters specified in the mask (list of allowable characters).
The following table summarizes the technical details of this function.
|Return Value:||Returns the length of the initial portion of the string that consist entirely of characters contained within the given mask.|
The basic syntax of the
strspn() function is given with:
The following example shows the
strspn() function in action.
<?php // Sample string $str = "Hello World!"; // Defining mask $mask = "Hi"; // Find length of initial portion of string matching mask echo strspn($str, $mask); ?>
strspn() function accepts the following parameters.
|string||Required. Specifies the string to work on.|
|mask||Required. Specifies the string containing every allowable characters.|
|start||Optional. Specifies the position in the string to start searching.|
|length||Optional. Specifies the portion of the string to search.|
Here're some more examples showing how
strspn() function actually works:
If string does not start with any characters specified in the mask, this function returns 0.
<?php echo strspn("Hello World!", "Wow"); ?>
The following example returns the length of the initial portion of the string that consists only of characters contained within "Her". Let's try it out and see how it works:
<?php echo strspn("Hello World!", "Her"); ?>
In the following example only the portion "World" of the string will be examined, because the position to start searching is 6 and the length of the string to search is 5.
<?php echo strspn("Hello World!", "Wow", 6, 5); ?>