PHP strspn() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, strspn() returns the length of the first part of a string that contains only characters from an allowed list.

It counts how many characters at the start match your list, and stops at the first character that does not.

Syntax

Call strspn() with the values you need. Start with this line.

PHP
$count = strspn("42 apples", "0123456789");
CallWhat it does
strspn("42 apples", "0123456789")Returns 2 (the leading digits).
strspn("abcde", "abc")Returns 3.
strspn("hello", "0123")Returns 0 because the first letter is not allowed.

Use offset and length to inspect only part of the string. The opposite function is strcspn(), which counts characters that are not in the list.

Library declarations
PHP
strspn(string $string, string $characters, int $offset = 0, ?int $length = null): int

Parameters

ParameterDescription
stringRequired. The string to check.
charactersRequired. The list of allowed characters.
offsetOptional. Where to start in the string. A negative value counts from the end.
lengthOptional. How many characters to examine. Default is null (to the end).

Return Value

Return ValueDescription
intThe length of the initial segment made only of allowed characters.

PHP Examples

Example 1 — Count Leading Digits

Find how many digits appear before the first non-digit.

PHP
<?php
$count = strspn("42 apples", "0123456789");

echo $count, PHP_EOL;

How It Works

  1. Pass the string and a list of digit characters.
  2. PHP checks "4" and "2", both allowed.
  3. The third character is a space, which is not allowed.
  4. It stops there and returns 2.

Example 2 — First Character Does Not Match

If the first character is not in the list, the result is 0.

PHP
<?php
echo strspn("hello", "0123"), PHP_EOL;
echo strspn("abcde", "abc"), PHP_EOL;

How It Works

  1. The first string starts with "h", which is not in "0123".
  2. strspn stops immediately and returns 0.
  3. The second string starts with a, b, c, which are all allowed.
  4. It counts 3 and stops at "d".

Example 3 — Check If Input Is All Digits

Compare the count with the full string length.

PHP
<?php
$inputs = ["12345", "12a45"];

foreach ($inputs as $input) {
    if (strspn($input, "0123456789") === strlen($input)) {
        echo $input, ": All digits", PHP_EOL;
    } else {
        echo $input, ": Not all digits", PHP_EOL;
    }
}

How It Works

  1. Create two test inputs.
  2. For each one, count the leading digits with strspn.
  3. If the count equals the full length, every character is a digit.
  4. "12a45" stops at the letter, so the counts do not match.

Frequently Asked Questions

strspn() counts how many characters at the start of a string are all found in an allowed list. It stops at the first character that is not in the list.
strspn() counts characters that are in the list. strcspn() counts characters that are not in the list.
Use strspn($s, '0123456789') === strlen($s). For simple checks, ctype_digit() is often easier.
They limit the part of the string that is checked. The count starts at the offset and looks at no more than length characters.
It returns 0. It never returns false.

Next: strstr()

strstr() finds the first occurrence of a string and returns the part of the string from th.

strstr() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful