Master PHP String Truncation: 5 Methods for Clean, Multilingual Output
Learn how to efficiently truncate strings in PHP using five approaches—substr for ASCII, mb_substr for multibyte characters, regex with preg_match, explode with array handling, and a custom function that adds ellipsis—while understanding each method's syntax, advantages, and limitations.
In PHP development, cutting strings is a frequent task, and choosing the right function can improve performance and avoid garbled output.
1. substr(): ASCII and fixed‑byte strings
substr()is a built‑in function suitable for single‑byte encodings such as ISO‑8859‑1. Its syntax is substr(string $string, int $start, ?int $length = null). Example:
$str = "Hello World";
echo substr($str, 0, 5); // HelloPros: fast execution because it is implemented in C.
Cons: does not support multibyte characters; misuse can cause garbled Chinese text.
2. mb_substr(): Multibyte‑safe truncation
For UTF‑8 and other multibyte encodings, use mb_substr(). Syntax:
mb_substr(string $str, int $start, ?int $length = null, ?string $encoding = null). Example:
$str = "你好世界Hello";
echo mb_substr($str, 0, 4, 'UTF-8'); // 你好世Pros: correctly handles Chinese, Japanese and other multibyte characters.
Note: the mbstring extension must be enabled.
3. preg_match() with regular expressions
When you need to extract patterns (e.g., URLs, emails, quoted text), combine preg_match() with a regex. Example extracting text inside quotes:
$str = 'name="张三"';
preg_match('/"([^\"]+)"/', $str, $matches);
echo $matches[1]; // 张三Pros: flexible for complex extraction rules.
Cons: slower than simple functions; regex errors can cause bugs.
4. explode() + array handling
If the string contains a known delimiter, split it with explode() and pick the needed part. Example obtaining a file extension:
$filename = "example.jpg";
$parts = explode('.', $filename);
$ext = end($parts); // jpgPros: clear logic for structured strings.
Note: if the delimiter is absent, the original string is returned as a single‑element array; further handling may be required.
5. Custom function for safe truncation with ellipsis
A reusable helper can truncate a string to a maximum length and append “...” when needed, while supporting multibyte encodings:
function truncateString($str, $maxLength, $encoding = 'UTF-8') {
if (mb_strlen($str, $encoding) > $maxLength) {
return mb_substr($str, 0, $maxLength, $encoding) . '...';
}
return $str;
}
echo truncateString("这是一段很长的中文描述", 8); // 这是一段很长...This approach balances readability and multilingual support, making it suitable for front‑end display scenarios.
Choosing the appropriate method depends on the string’s language and format: use substr for pure English, mb_substr for Chinese or other multibyte text, explode when a delimiter exists, and regular expressions for complex patterns.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
