Content · Programming · PHP

PHP strcspn() Function: Usage and Examples

Counts the initial bytes in a string before any character from a disallowed character set appears.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

PHP strcspn() counts the initial bytes in a string before any character from a disallowed character set appears. The examples below show how the call behaves, where it is useful, and which limitations matter.

What Is strcspn() in PHP?


Counts the initial bytes in a string before any character from a disallowed character set appears.

Its call structure is:

strcspn($string, $characters, [$offset], [$length])

The mask lists bytes that stop the scan; optional offset and length limit the examined region.

Example 1: Basic Usage


This example introduces strcspn with a direct call and a small, readable input.

<?php

echo strcspn("[email protected]", "@ ");

Output:

4

Counts the initial bytes in a string before any character from a disallowed character set appears. The mask is a set of individual bytes, not a whole substring or regular expression.

Example 2: Another Behavior or Input


Compare this input with the previous example: changing the data or option can affect how strcspn behaves.

<?php

echo strcspn("A9-7", "0123456789");

Output:

1

The changed input illustrates why output may differ with the chosen values or environment. The mask is a set of individual bytes, not a whole substring or regular expression.

Practical Example: Using the Feature


The following example incorporates strcspn into a workflow. Values such as paths, connection handles and configuration options must come from trusted application settings.

<?php

$candidate = "alpha-123"; $prefixLength = strcspn($candidate, "0123456789"); echo substr($candidate, 0, $prefixLength);

Output:

alpha-

In a real application, check each operation result, validate values before use and clean up acquired resources. The mask is a set of individual bytes, not a whole substring or regular expression.

Important Behavior and Edge Cases


  • Purpose: Counts the initial bytes in a string before any character from a disallowed character set appears.
  • Key limitation: The mask is a set of individual bytes, not a whole substring or regular expression.
  • Failure handling: Check return values and handle invalid inputs and unavailable operating-system or extension features where relevant.

Comparison With Related Functions


Function or ApproachBest Fit
strcspn()Counts the initial bytes in a string before any character from a disallowed character set appears.
strspn()Counts leading bytes that belong to an allowed set, the opposite of stopping at disallowed bytes.

Common Beginner Mistakes


MistakeCorrect Approach
The mask is a set of individual bytes, not a whole substring or regular expression.The correct approach depends on this behavior: The mask is a set of individual bytes, not a whole substring or regular expression.
Ignoring errors or cleanup after the operation.Validate inputs and check each result; close files, restore settings or release resources when the operation requires it.

Key Takeaways


  • Counts the initial bytes in a string before any character from a disallowed character set appears.
  • The mask is a set of individual bytes, not a whole substring or regular expression.
  • Use the examples as starting points, and adapt environmental paths, resources and permissions rather than assuming fixed results.

strcspn is useful when its specific behavior fits the problem. The safest implementations combine the correct API with input validation, error checking and resource cleanup.

Share this resourceSend this page to someone who may find it useful.