Of course. Let's break down the cn_substrr function from the Dedecms (织梦CMS) system.

Summary
cn_substrr is a custom PHP function used in the Dedecms content management system. Its primary purpose is to safely cut a string of text to a specified length. The key feature that makes it different from standard PHP functions like substr() is that it is "multibyte-safe," meaning it correctly handles character sets like UTF-8, which are common in Chinese websites.
Function Signature
function cn_substrr($str, $start, $len = false, $suffix = true)
Parameters Explained
Let's look at what each parameter does:
| Parameter | Type | Description |
|---|---|---|
$str |
string |
(Required) The input string that you want to cut. This is the main text, which could be a title, article body, etc. |
$start |
integer |
(Required) The starting position for the cut. This is a zero-based index. 0 means start from the beginning of the string. |
$len |
integer |
(Optional) The length of the substring you want to extract. If you omit this parameter (or set it to false), the function will cut the string from the $start position to the very end. |
$suffix |
boolean |
(Optional) A flag to add a suffix (like ) to the end of the cut string. The default value is true. If true, and the string was actually cut (i.e., the original string was longer than $len), it will append an ellipsis (). If false, no suffix is added. |
How It Works (The "Why")
The standard PHP function substr() has a major limitation: it treats each character as a single byte. In character sets like UTF-8, a single character (especially a Chinese character) can be made up of multiple bytes.
For example:

- The letter
Ais 1 byte. - The Chinese character
中is 3 bytes in UTF-8.
If you use substr('你好世界', 0, 4), PHP will cut the first 4 bytes. This might cut a Chinese character in half, resulting in garbled text like .
cn_substrr solves this problem. It uses a multibyte string function, mb_strlen(), to correctly count characters, not bytes. This ensures it never cuts a character in the middle, preserving the integrity of the text.
Code Example
Let's see it in action.
Scenario: Truncating an Article Title
Imagine you have a long article title and you want to display it in a list, limiting it to 20 characters.
<?php
// This is the actual source code of the function, for context
function cn_substrr($str, $start, $len = false, $suffix = true) {
if ($len === false) {
$len = cn_strlen($str) - $start;
}
$slice = cn_substr($str, $start, $len);
if ($suffix && $slice != $str) {
$slice .= '...';
}
return $slice;
}
// NOTE: The actual Dedecms system also has a cn_strlen() and cn_substr() function.
// For this example to work standalone, we'll use PHP's built-in mb_* functions.
// A more faithful standalone version would look like this:
function cn_substrr_standalone($str, $start, $len = false, $suffix = true) {
// Ensure the string is in UTF-8 for proper multibyte handling
$str = mb_convert_encoding($str, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5');
if ($len === false) {
$len = mb_strlen($str) - $start;
}
$slice = mb_substr($str, $start, $len, 'UTF-8');
if ($suffix && $slice != $str) {
$slice .= '...';
}
return $slice;
}
// --- USAGE EXAMPLE ---
// A long article title with Chinese characters
$long_title = "深入解析DedeCms内容管理系统的核心架构与开发技巧";
// 1. Cut to 10 characters, starting from the beginning
$short_title_1 = cn_substrr_standalone($long_title, 0, 10);
echo "Cut to 10 chars: " . $short_title_1 . "<br>";
// Output: Cut to 10 chars: 深入解析Dede...
// 2. Cut to 15 characters, starting from the beginning
$short_title_2 = cn_substrr_standalone($long_title, 0, 15);
echo "Cut to 15 chars: " . $short_title_2 . "<br>";
// Output: Cut to 15 chars: 深入解析DedeCms...
// 3. Cut to 10 characters, but disable the suffix
$short_title_3 = cn_substrr_standalone($long_title, 0, 10, false);
echo "Cut to 10 chars (no suffix): " . $short_title_3 . "<br>";
// Output: Cut to 10 chars (no suffix): 深入解析Dede
// 4. Cut from a specific position (e.g., skip the first 5 characters)
$short_title_4 = cn_substrr_standalone($long_title, 5, 10);
echo "Cut from position 5, length 10: " . $short_title_4 . "<br>";
// Output: Cut from position 5, length 10: 析DedeCms内容...
Modern Alternatives
While cn_substrr is a perfectly good utility function, modern PHP development has made it largely redundant. The PHP core now includes built-in multibyte string functions that are just as capable.
If you are writing new code, you should use the native mb_* functions:
mb_strlen($string, 'UTF-8'): To get the character length of a string.mb_substr($string, $start, $length, 'UTF-8'): To get a substring safely.
Example using modern PHP:
$long_title = "深入解析DedeCms内容管理系统的核心架构与开发技巧";
// Get the first 15 characters using modern PHP
$short_title = mb_substr($long_title, 0, 15, 'UTF-8');
if (mb_strlen($long_title, 'UTF-8') > 15) {
$short_title .= '...';
}
echo "Modern approach: " . $short_title;
// Output: Modern approach: 深入解析DedeCms...
Conclusion
| Feature | cn_substrr (DedeCms) |
mb_substr() (Modern PHP) |
|---|---|---|
| Purpose | Truncate multibyte strings safely. | Truncate multibyte strings safely. |
| Origin | Custom function within the DedeCms framework. | Built-in PHP function since PHP 4.3.0. |
| Reliability | Reliable, but dependent on the DedeCms environment. | Standard, reliable, and recommended. |
| Usage | cn_substrr($str, 0, 15); |
mb_substr($str, 0, 15, 'UTF-8'); |
In short, cn_substrr is a DedeCms-specific helper function that was essential for handling Chinese text before robust multibyte support was standard in PHP. Today, its functionality is better served by the native mb_substr() function. However, if you are maintaining or working within a DedeCms project, you will undoubtedly encounter and use cn_substrr frequently.
