Backend Development 5 min read

Using PHP strspn() Function for String Length Measurement and Filtering

This article explains PHP's strspn() function, detailing its syntax, parameters, return values, and demonstrates three practical examples for measuring consecutive character length, checking character set inclusion, and filtering strings, helping developers handle string processing more flexibly.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP strspn() Function for String Length Measurement and Filtering

PHP is a widely used scripting language for web development, offering powerful string handling capabilities. One of its built‑in functions is strspn() , which calculates the length of a segment of characters in a string that matches a given character set.

Syntax :

<code>int strspn ( string $str1 , string $str2 [, int $start [, int $length ]] )</code>

Parameters :

$str1 : The input string to be examined.

$str2 : The set of characters to match against.

$start (optional): The position in $str1 where matching starts, default is 0.

$length (optional): The maximum length to examine, default is the length of the string.

The function returns the length of the initial segment of $str1 that consists only of characters found in $str2 . If no characters match, it returns 0.

Basic Usage Examples

Example 1: Measuring consecutive characters

<code>&lt;?php
$str = "abc123";
$chars = "abcdefghijklmnopqrstuvwxyz";

$length = strspn($str, $chars);

echo "匹配到的连续字符的长度为:" . $length;
?&gt;</code>

Output: 匹配到的连续字符的长度为:3 . The function counts the leading letters "abc" before encountering a non‑matching character.

Example 2: Checking if a string contains only allowed characters

<code>&lt;?php
$str = "abc123";
$chars = "abcdefghijklmnopqrstuvwxyz";

$length = strspn($str, $chars);

if ($length == strlen($str)) {
    echo "字符串只包含指定字符集合中的字符。";
} else {
    echo "字符串包含了非指定字符集合中的字符。";
}
?&gt;</code>

Output: 字符串包含了非指定字符集合中的字符。 . Since the matched length (3) is less than the total string length (6), the string contains characters outside the allowed set.

Example 3: Filtering out unwanted characters

<code>&lt;?php
$str = "abc123";
$chars = "abcdefghijklmnopqrstuvwxyz";

$newStr = substr($str, strspn($str, $chars));

echo "过滤后的字符串为:" . $newStr;
?&gt;</code>

Output: 过滤后的字符串为:123 . By using the length returned by strspn() as the offset for substr() , the leading letters are removed, leaving only the numeric part.

Summary

The strspn() function in PHP calculates the length of a leading substring that consists solely of characters from a specified set. It can be used to measure consecutive characters, verify that a string contains only allowed characters, or filter out unwanted characters, providing developers with flexible string‑processing tools.

TutorialString Manipulationphp-functionsstrspn
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.