PHP strspn() counts how many initial bytes are members of an allowed-character set. The examples below show how the call behaves, where it is useful, and which limitations matter.
What Is strspn() in PHP?
Counts how many initial bytes are members of an allowed-character set.
Its call structure is:
strspn($string, $characters, [$offset], [$length])
The first string is examined against an allowed byte-character mask, optionally starting at an offset and for a restricted length.
Example 1: Basic Usage
This example introduces strspn with a direct call and a small, readable input.
<?php
echo strspn("ABC123", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
Output:
3
Counts how many initial bytes are members of an allowed-character set. The character set defines allowed bytes, not an exact token or regular expression.
Example 2: Another Behavior or Input
Compare this input with the previous example: changing the data or option can affect how strspn behaves.
<?php
echo strspn("123-ABC", "0123456789");
Output:
3
The changed input illustrates why output may differ with the chosen values or environment. The character set defines allowed bytes, not an exact token or regular expression.
Practical Example: Using the Feature
The following example incorporates strspn into a workflow. Values such as paths, connection handles and configuration options must come from trusted application settings.
<?php
$productCode = "AB12-55"; $prefixSize = strspn($productCode, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"); echo substr($productCode, 0, $prefixSize);
Output:
AB
In a real application, check each operation result, validate values before use and clean up acquired resources. The character set defines allowed bytes, not an exact token or regular expression.
Important Behavior and Edge Cases
- Purpose: Counts how many initial bytes are members of an allowed-character set.
- Key limitation: The character set defines allowed bytes, not an exact token 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 Approach | Best Fit |
|---|---|
strspn() | Counts how many initial bytes are members of an allowed-character set. |
strcspn() | Measures the initial span that excludes characters from a specified set. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| The character set defines allowed bytes, not an exact token or regular expression. | The correct approach depends on this behavior: The character set defines allowed bytes, not an exact token 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 how many initial bytes are members of an allowed-character set.
- The character set defines allowed bytes, not an exact token or regular expression.
- Use the examples as starting points, and adapt environmental paths, resources and permissions rather than assuming fixed results.
strspn is useful when its specific behavior fits the problem. The safest implementations combine the correct API with input validation, error checking and resource cleanup.