strspn() Funtion in PHP 8.2, PHP 8.3 & PHP 8.4


The `strspn()` function in PHP calculates the length of the initial segment of a string that consists entirely of characters contained within a specified mask.PHP 8. PHP 8.1,PHP 8.2,PHP 8.3 and PHP 8.4.Syntax
<?phpstrspn(string $string,string $characters,int $offset = 0,?int $length = null): int?>
Parameters1.`$string` - The input string to examine2.`$characters` - The list of allowed characters3.`$offset` (optional) - The position in `$string` to start searching4.`$length` (optional) - The length of the segment to examineExample 1: Basic Usage
<?php$string = "123abc456";$mask = "0123456789";$length = strspn($string, $mask);echo $length; // Output: 3 (because "123" are all in the mask)?>
Notes1. The function is binary-safe (handles binary data correctly).2. In PHP 8.2, the function works consistently with multibyte characters (like UTF-8), but for complex Unicode handling, you might still want to use mbstring functions.3. The function returns 0 if no characters match or if the offset is beyond the string length.The `strspn()` function is particularly useful for validation, parsing, and string analysis tasks where you need to check which characters from a specific set appear at the start of a string.