When working with PHP, you may need to extract characters without splitting UTF-8 sequences. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.
mb_substr: Start and length count characters rather than raw bytes; negative positions are supported.
What Is mb_substr in PHP?
Extract characters without splitting UTF-8 sequences. Start and length count characters rather than raw bytes; negative positions are supported.
Syntax:
mb_substr(string $string, int $start, ?int $length = null, ?string $encoding = null): string
Parameters and return value: The signature above identifies the arguments and return type where the API has a single call. For topic guides, follow the sequence of calls in the examples. Always distinguish a returned value from an object or variable modified by reference.
Important: Character slices can still split grapheme sequences containing multiple code points.
Example 1: Basic Usage
This example introduces the operation with a short and predictable input.
<?php
echo mb_substr("Café menu", 0, 4, "UTF-8");
The code focuses on the main operation. Start and length count characters rather than raw bytes; negative positions are supported.
Example 2: Another Case
Now compare a different input, locale, date, or option to see another part of the behavior.
<?php
echo mb_substr("abcdef", -3, null, "UTF-8");
What changes? Character slices can still split grapheme sequences containing multiple code points.
Practical Example: Applying It in an Application
Use the following separate example as a starting point for a small application task. Check any extension or environment requirements below before running it.
<?php
$titles = ["Introduction to PHP", "Working with Unicode"];
foreach ($titles as $title) {
echo mb_substr($title, 0, 12, "UTF-8") . PHP_EOL;
}
The practical example demonstrates how to apply this operation to multiple values, a reusable task, or a validated input. Start and length count characters rather than raw bytes; negative positions are supported.
Important Behavior and Edge Cases
- Core behavior: Start and length count characters rather than raw bytes; negative positions are supported.
- Inputs and results: Character slices can still split grapheme sequences containing multiple code points.
- Alternatives: grapheme_substr() is preferable when preserving user-perceived characters.
- Compatibility: mbstring is required for mb_* functions.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
mb_substr | Extract characters without splitting utf-8 sequences. |
| Related PHP approach | grapheme_substr() is preferable when preserving user-perceived characters. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Using an incorrect input or option | Character slices can still split grapheme sequences containing multiple code points. |
| Confusing this API with a related operation | grapheme_substr() is preferable when preserving user-perceived characters. |
| Assuming output is identical across environments | Check return values and required extensions or versions. mbstring is required for mb_* functions. |
Key Takeaways
- mb_substr helps you extract characters without splitting UTF-8 sequences.
- Start and length count characters rather than raw bytes; negative positions are supported.
- Character slices can still split grapheme sequences containing multiple code points.
Use mb_substr when you need to extract characters without splitting UTF-8 sequences. Start with the first example, compare the second, and adapt the practical example to your inputs. Confirm the return value and environment requirements before adding it to production code.