Current Location: Home> Latest Articles> How to Use iconv_substr with strlen to Control String Length: Practical Tips

How to Use iconv_substr with strlen to Control String Length: Practical Tips

gitbox 2025-09-08

In PHP programming, handling string length—especially with multibyte character sets such as Chinese, Japanese, and Korean—can be challenging. Since strlen() and substr() are designed for single-byte characters, they may produce unexpected results when used with multibyte characters. Fortunately, PHP offers the powerful iconv library, and iconv_substr() can effectively solve this problem. Today, we will explore how to use iconv_substr() together with strlen() to precisely control string length.

Why Use iconv_substr?

With traditional substr() and strlen() functions, if we handle multibyte characters (such as Chinese), strlen() calculates the number of bytes rather than the number of characters. This can lead to unexpected string lengths. For example:

<span><span><span class="hljs-variable">$string</span></span><span> = </span><span><span class="hljs-string">"你好,世界!"</span></span><span>;
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-title function_ invoke__">strlen</span></span><span>(</span><span><span class="hljs-variable">$string</span></span><span>); </span><span><span class="hljs-comment">// Outputs 18</span></span><span>
</span></span>

Here, strlen() returns the byte count instead of the character count. Each Chinese character uses 3 bytes, so this string has 6 characters, but strlen() incorrectly returns 18 bytes.

On the other hand, iconv_substr() is specifically designed for multibyte characters and can correctly calculate the number of characters rather than bytes, allowing precise control over string length.

How to Use iconv_substr with strlen?

1. Count the Number of Characters in a String

First, we use iconv_strlen() to count the number of characters in the string instead of bytes. iconv_strlen() returns the actual character count regardless of whether the string uses a single-byte or multibyte character set.

<span><span><span class="hljs-variable">$string</span></span><span> = </span><span><span class="hljs-string">"你好,世界!"</span></span><span>;
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-title function_ invoke__">iconv_strlen</span></span><span>(</span><span><span class="hljs-variable">$string</span></span><span>, </span><span><span class="hljs-string">&#039;UTF-8&#039;</span></span><span>); </span><span><span class="hljs-comment">// Outputs 6</span></span><span>
</span></span>

2. Use iconv_substr to Extract a Substring

Next, we can use iconv_substr() to extract a substring. Its parameters are similar to substr(), but it correctly handles multibyte character sets. By combining iconv_strlen() and iconv_substr(), we can precisely control the number of characters in the extracted substring.

<span><span><span class="hljs-variable">$string</span></span><span> = </span><span><span class="hljs-string">"你好,世界!"</span></span><span>;
</span><span><span class="hljs-variable">$length</span></span><span> = </span><span><span class="hljs-number">3</span></span><span>;
</span><span><span class="hljs-variable">$substring</span></span><span> = </span><span><span class="hljs-title function_ invoke__">iconv_substr</span></span><span>(</span><span><span class="hljs-variable">$string</span></span><span>, </span><span><span class="hljs-number">0</span></span><span>, </span><span><span class="hljs-variable">$length</span></span><span>, </span><span><span class="hljs-string">&#039;UTF-8&#039;</span></span><span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-variable">$substring</span></span><span>; </span><span><span class="hljs-comment">// Outputs "你好,"</span></span><span>
</span></span>

In this example, iconv_substr() starts from the first character of the string and extracts 3 characters. Here, length refers to the number of characters, not bytes, avoiding the inaccuracies that may occur with substr().

3. Dynamically Control String Length

Sometimes we need to dynamically control string length based on certain conditions, such as limiting the number of displayed characters. Using iconv_substr() in combination with iconv_strlen() makes this straightforward.

<span><span><span class="hljs-function"><span class="hljs-keyword">function</span></span></span><span> </span><span><span class="hljs-title">truncate_string</span></span><span>(</span><span><span class="hljs-variable">$string</span></span><span>, </span><span><span class="hljs-variable">$max_length</span></span><span>) {
    </span><span><span class="hljs-variable">$length</span></span><span> = </span><span><span class="hljs-title function_ invoke__">iconv_strlen</span></span><span>(</span><span><span class="hljs-variable">$string</span></span><span>, </span><span><span class="hljs-string">&#039;UTF-8&#039;</span></span><span>);
    </span><span><span class="hljs-keyword">if</span></span><span> (</span><span><span class="hljs-variable">$length</span></span><span> > </span><span><span class="hljs-variable">$max_length</span></span><span>) {
        </span><span><span class="hljs-keyword">return</span></span><span> </span><span><span class="hljs-title function_ invoke__">iconv_substr</span></span><span>(</span><span><span class="hljs-variable">$string</span></span><span>, 0, </span><span><span class="hljs-variable">$max_length</span></span>, </span><span><span class="hljs-string">&#039;UTF-8&#039;</span></span><span>) . </span><span><span class="hljs-string">&#039;...&#039;</span></span><span>;
    } else {
        </span><span><span class="hljs-keyword">return</span></span><span> </span><span><span class="hljs-variable">$string</span></span><span>;
    }
}
<p></span>$string = "你好,世界!这是一个测试字符串。";<br>
echo truncate_string($string, </span>10); // Outputs "你好,世界!..."<br>
</span>

In this example, the truncate_string() function trims the string to the specified maximum length and appends an ellipsis at the end.

Conclusion

By using iconv_substr() and iconv_strlen(), we can effectively address the challenge of controlling string length in multibyte character sets. These functions allow us to manipulate strings precisely by character rather than byte, avoiding the inaccuracies of strlen() and substr() with multibyte characters. For projects that involve multiple languages, especially those including Chinese, Japanese, or Korean characters, these functions are extremely useful tools.