PHP strcspn() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, strcspn() counts how many characters at the start of a string do not match any character from a given list.

It stops at the first character that is in the list.

Syntax

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

PHP
$count = strcspn($text, "aeiou");
CallWhat it does
strcspn("hello world", "o")Returns 4.
strcspn("abc", "xyz")Returns 3 (whole string).

Think of it as: how far can I walk before I hit one of these characters? It is the opposite of strspn().

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

Parameters

ParameterDescription
stringRequired. The string to check.
charactersRequired. A string containing every character that should stop the count.
offsetOptional. Where to start in the string. Default is 0.
lengthOptional. How many characters to check from the offset. Default is to the end.

Return Value

Return ValueDescription
intThe length of the starting segment that has none of the listed characters.

PHP Examples

Example 1 — Count Before a Character

Find how many characters come before the letter o.

PHP
<?php
$count = strcspn("hello world", "o");

echo $count, PHP_EOL;

How It Works

  1. Pass a string and a stop list. The list is just "o".
  2. Walk from the start. h, e, l, l are not in the list.
  3. Stop at o. That is the fifth character.
  4. Return 4. Four characters came before it.

Example 2 — Use Offset and No Match

Start later in the string, or search for characters that are not there.

PHP
<?php
echo strcspn("hello, world", "ow", 5), PHP_EOL;
echo strcspn("abc", "xyz"), PHP_EOL;

How It Works

  1. Start at offset 5. The text from there is , world.
  2. Stop at w. The comma and space come first, so the count is 2.
  3. Search for missing characters. None of x, y, z exist.
  4. Get the full length. The result is 3.

Example 3 — Split Letters from Digits

Find where the first digit starts.

PHP
<?php
$text = "abc123";
$pos = strcspn($text, "0123456789");

echo substr($text, 0, $pos), PHP_EOL;
echo substr($text, $pos), PHP_EOL;

How It Works

  1. List all digits. Each digit is a stop character.
  2. Count the letters. strcspn() returns 3.
  3. Use substr() for the first part. It gives abc.
  4. Use substr() for the rest. It gives 123.

Frequently Asked Questions

strcspn() returns the length of the first part of a string that contains none of the characters in a given list.
strcspn(string $string, string $characters, int $offset = 0, ?int $length = null): int. The last two arguments are optional.
strspn() counts characters that are in the list. strcspn() counts characters that are not in the list.
No. It is treated as a set of single characters. Any one of them will stop the count.
The result is the length of the checked part, usually the whole string.

Next: strip_tags()

strip_tags() removes HTML and PHP tags from a string.

strip_tags() 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