When working with PHP, you may need to measure estimated text display width. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.
mb_strwidth: Characters in East Asian scripts can occupy multiple terminal columns.
What Is mb_strwidth in PHP?
Measure estimated text display width. Characters in East Asian scripts can occupy multiple terminal columns.
Syntax:
mb_strwidth(string $string, ?string $encoding = null): int
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: Estimated display width is not a pixel measurement and does not account for every terminal or font.
Example 1: Basic Usage
This example introduces the operation with a short and predictable input.
<?php
echo mb_strwidth("hello", "UTF-8");
The code focuses on the main operation. Characters in East Asian scripts can occupy multiple terminal columns.
Example 2: Another Case
Now compare a different input, locale, date, or option to see another part of the behavior.
<?php
echo mb_strwidth("こんにちは", "UTF-8");
What changes? Estimated display width is not a pixel measurement and does not account for every terminal or font.
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
$columns = ["Name", "Status", "説明"];
foreach ($columns as $column) {
echo $column . " => " . mb_strwidth($column, "UTF-8") . PHP_EOL;
}
The practical example demonstrates how to apply this operation to multiple values, a reusable task, or a validated input. Characters in East Asian scripts can occupy multiple terminal columns.
Important Behavior and Edge Cases
- Core behavior: Characters in East Asian scripts can occupy multiple terminal columns.
- Inputs and results: Estimated display width is not a pixel measurement and does not account for every terminal or font.
- Alternatives: mb_strlen() counts characters and mb_strimwidth() truncates by width.
- Compatibility: mbstring is required for mb_* functions.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
mb_strwidth | Measure estimated text display width. |
| Related PHP approach | mb_strlen() counts characters and mb_strimwidth() truncates by width. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Using an incorrect input or option | Estimated display width is not a pixel measurement and does not account for every terminal or font. |
| Confusing this API with a related operation | mb_strlen() counts characters and mb_strimwidth() truncates by width. |
| Assuming output is identical across environments | Check return values and required extensions or versions. mbstring is required for mb_* functions. |
Key Takeaways
- mb_strwidth helps you measure estimated text display width.
- Characters in East Asian scripts can occupy multiple terminal columns.
- Estimated display width is not a pixel measurement and does not account for every terminal or font.
Use mb_strwidth when you need to measure estimated text display width. 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.