PHP functions strcspn() and strspn()

Yongyao Yan
3 min readMay 4, 2022

Function strcspn()

The function strcspn() finds the length of initial segment not matching mask.

In the code snippet below, we want to find the prefix length of a UK post code. CF34 9LH is the post code string and the disallowed character list is 0123456789. The function strcspn() is called to find the length of the initial segment that does not contain any numbers. So, CF is the string segment and the length is 2.

You can then easily get the prefix of a post code like this:

You can also use the function strcspn() for checking a file name whether it contains any invalid characters.

In the above code snippet, since the file name string $str contains invalid characters, the length of the initial segment, i.e. filename123, is not the same with the whole file name string. The error message is therefore printed out.

Fuction strspn()

On the contrary, the function strspn() gets the length of the initial segment of a string, which consists entirely of characters contained within a given mask.

In the example below, the function strspn() is called to get the length of the substring that contains numbers in the given mask 0123456789.

You can also set the offset position to find the substring like this:

It starts at position 3 to find the substring 1234. Therefore, the length is 4.

Case-sensitive search

Both strcspn() and strspn() are case-sensitive. If you define the mask as abc, the function strcspn() cannot match ABC in the code snippet below. So, the length of the whole string, i.e. 12345ABC, is returned.

About the function strspn(), you need to define the uppercase or the lowercase of a character that you want to match. In order to extract the first part of the sentence as below, you need to define the mask as ' cCdDehiopstT'. And don't forget to include a space in it.

Resources

Thanks for reading! To find more programming tutorials, please visit: CodeBilby.com

--

--

Yongyao Yan

I am a programmer and a technical writer. To find more programming tutorials, please visit my website: https://www.codebilby.com