Saturday, 30 April 2022

PHP functions strcspn() and strspn()

PHP 

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.

$p = strcspn('CF34 9LH', '0123456789');
echo "The length is " . $p;
//The length is 2

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

$postCode = 'CF34 9LH';
$postPrefix = substr($postCode, 0, strcspn($postCode, '0123456789'));
echo "The prefix is " . $postPrefix;
//The prefix is CF

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

$str = "filename123$/@456";
$invalidChars = "\\/*@$";
$validLen = strcspn($str, $invalidChars);

if ($validLen != strlen($str)) {
    echo "Invalid character found at position " . $validLen;
    //Invalid character found at position 11
}

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.

$p = strspn('12345abcde', '0123456789');
echo "The length is " . $p; //The length is 5

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

$p = strspn('abc1234defg', '0123456789', 3);
echo "The length is " . $p; //The length is 4

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.

$p = strcspn('12345ABC', 'abc');
echo "The length is " . $p; //The length is 8

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.

$str = "The post code is CD45 9LH";
$subStr = substr($str, 0, strspn($str, ' cCdDehiopstT'));

echo "The substring is: " . $subStr; 
//The substring is: The post code is CD

Resources



Search