Current Location: Home> Latest Articles> How to Use the iconv_substr Function? A Comprehensive Guide to Its Basic Usage and Applications

How to Use the iconv_substr Function? A Comprehensive Guide to Its Basic Usage and Applications

gitbox 2025-08-04

iconv_substr is a powerful PHP function used for extracting substrings, especially suitable for multibyte character sets like UTF-8 and GBK. Unlike the traditional substr function, iconv_substr correctly processes strings containing multibyte characters, performing exceptionally well when handling Chinese, Japanese, Korean, and other non-Latin character sets.

1. Basic Syntax of the iconv_substr Function

<span><span><span class="hljs-title function_ invoke__">iconv_substr</span></span><span>(</span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$str</span></span><span>, </span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-variable">$start</span></span><span>, </span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-variable">$length</span></span><span> = ?, </span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$encoding</span></span><span> = ?): </span><span><span class="hljs-keyword">string</span></span><span>|</span><span><span class="hljs-literal">false</span></span><span>
</span></span>
  • $str: The input original string, usually the string you want to extract from.

  • $start: The starting position for extraction. It can be negative, which means counting from the end of the string.

  • $length: The length of the substring to extract, specifying how many characters to retrieve. If omitted, it defaults to extracting until the end of the string.

  • $encoding: The character encoding, typically UTF-8 or GBK. If not specified, the system default encoding is used.

2. Usage Examples

1. Basic Example

<span><span><span class="hljs-variable">$str</span></span><span> = </span><span><span class="hljs-string">"Hello, 你好,世界!"</span></span><span>;
</span><span><span class="hljs-variable">$result</span></span><span> = </span><span><span class="hljs-title function_ invoke__">iconv_substr</span></span><span>(</span><span><span class="hljs-variable">$str</span></span><span>, </span><span><span class="hljs-number">7</span></span><span>, </span><span><span class="hljs-number">3</span></span><span>, </span><span><span class="hljs-string">"UTF-8"</span></span><span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-variable">$result</span></span>;  </span><span><span class="hljs-comment">// Output: 你好</span></span>
</span></span>

In the example above, we start extracting from the 7th character, taking 3 characters. Since it is UTF-8 encoded, iconv_substr correctly handles the Chinese characters.

2. Using Negative Start Position

<span><span><span class="hljs-variable">$str</span></span><span> = </span><span><span class="hljs-string">"Hello, 你好,世界!"</span></span><span>;
</span><span><span class="hljs-variable">$result</span></span><span> = </span><span><span class="hljs-title function_ invoke__">iconv_substr</span></span><span>(</span><span><span class="hljs-variable">$str</span></span><span>, -</span><span><span class="hljs-number">3</span></span><span>, </span><span><span class="hljs-number">3</span></span><span>, </span><span><span class="hljs-string">"UTF-8"</span></span><span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-variable">$result</span></span>;  </span><span><span class="hljs-comment">// Output: 世界</span></span>
</span></span>

In this example, -3 means to start extracting from the third last character of the string, resulting in "世界".

3. Extracting Until the End of the String

<span><span><span class="hljs-variable">$str</span></span><span> = </span><span><span class="hljs-string">"PHP 编程语言"</span></span><span>;
</span><span><span class="hljs-variable">$result</span></span><span> = </span><span><span class="hljs-title function_ invoke__">iconv_substr</span></span><span>(</span><span><span class="hljs-variable">$str</span></span><span>, </span><span><span class="hljs-number">4</span></span><span>, </span><span><span class="hljs-number">10</span></span><span>, </span><span><span class="hljs-string">"UTF-8"</span></span><span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-variable">$result</span></span>;  </span><span><span class="hljs-comment">// Output: 编程语言</span></span>
</span></span>

Here, we specify to start extracting from the 4th character, but do not specify the length, so it extracts until the end of the string by default.

3. Differences Between iconv_substr and substr

substr is a common PHP function suitable for single-byte character sets (like ASCII). However, it may produce errors when handling multibyte character sets. For example, using substr to extract Chinese characters might result in garbled text or incomplete characters. On the other hand, iconv_substr is designed for multibyte character sets and ensures accurate extraction without garbling.

Example:

<span><span><span class="hljs-variable">$str</span></span><span> = </span><span><span class="hljs-string">"PHP中文教程"</span></span><span>;
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-title function_ invoke__">substr</span></span><span>(</span><span><span class="hljs-variable">$str</span></span><span>, </span><span><span class="hljs-number">3</span></span><span>, </span><span><span class="hljs-number">2</span></span><span>);  </span><span><span class="hljs-comment">// May output garbled text</span></span>
</span></span>
<span><span><span class="hljs-variable">$str</span></span><span> = </span><span><span class="hljs-string">"PHP中文教程"</span></span><span>;
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-title function_ invoke__">iconv_substr</span></span><span>(</span><span><span class="hljs-variable">$str</span></span><span>, </span><span><span class="hljs-number">3</span></span><span>, </span><span><span class="hljs-number">2</span></span><span>, </span><span><span class="hljs-string">"UTF-8"</span></span><span>);  </span><span><span class="hljs-comment">// Correct output: 中文</span></span>
</span></span>

4. Application Scenarios

The iconv_substr function is very useful in many practical scenarios, especially when dealing with multibyte character sets. Some common application scenarios include:

1. Extracting Chinese Strings

When developing multilingual websites or applications, it's often necessary to extract a certain length of Chinese text. iconv_substr ensures accurate extraction of complete characters without causing garbled text or errors.

2. Extracting File Names or Paths

When handling file names containing Chinese, Japanese, or other non-Latin characters, using iconv_substr ensures the file names or paths are correctly extracted without garbling.

3. Extracting Fields from Databases

When interacting with databases, iconv_substr can be used to extract text fields, especially for storing multilingual text (such as Chinese or Japanese), ensuring no garbled or incomplete characters.

4. Paginating Strings for Display

In some applications, long texts need to be paginated for display. iconv_substr helps accurately extract the text for each page, preventing situations where a character is cut in half.

5. Conclusion

iconv_substr is a highly practical PHP function, especially suitable for handling strings with multibyte character sets. It provides better support and ensures accuracy when processing Chinese, Japanese, Korean, and other multibyte characters. Mastering the basic usage and application scenarios of this function is crucial for developing internationalized PHP applications.