Current Location: Home> Latest Articles> How to Specify the Character Set Parameter for Substring Extraction with iconv_substr? Detailed Instructions

How to Specify the Character Set Parameter for Substring Extraction with iconv_substr? Detailed Instructions

M66 2025-07-18

In PHP development, when working with multi-byte character set strings (such as Chinese, Japanese, Korean, etc.), developers often face issues with string extraction. Using the regular substr() function to extract strings is based on byte operations, which can lead to garbled results or truncating multi-byte characters. To resolve this issue, PHP provides the iconv_substr() function, which supports extracting multi-byte character sets and allows specifying the character set encoding.

This article will explain in detail how to use the iconv_substr() function to specify the character set parameter for string extraction, along with practical examples.


1. What is iconv_substr()?

iconv_substr() is a function in PHP used to extract substrings, relying on the iconv extension. It can correctly extract multi-byte strings according to the specified character set, avoiding garbled output.

The function prototype is as follows:

string iconv_substr ( string $str , int $offset [, int $length = NULL [, string $charset = ini_get("iconv.internal_encoding") ]] )
  • $str: The input string.

  • $offset: The starting position of the substring (measured in characters, not bytes).

  • $length: The length of the substring, defaulting to the rest of the string.

  • $charset: The character set encoding of the string, such as UTF-8, GBK, etc.


2. Why Specify the Character Set Parameter?

Different character encodings lead to different byte lengths. For example, a Chinese character typically occupies 3 bytes in UTF-8 encoding but only 2 bytes in GBK encoding. If the correct character set is not specified, iconv_substr() cannot correctly identify the boundaries of the string, leading to incorrect extraction positions or garbled output.


3. Detailed Instructions

3.1 Basic Usage Example

Assume you have a UTF-8 encoded Chinese string:

<?php
$str = "Welcome to PHP string extraction operation.";
$substr = iconv_substr($str, 3, 5, "UTF-8");
echo $substr;
?>

Explanation:

  • Extract starting from the 4th character (since $offset is 3, 0-based).

  • Extract 5 characters.

  • Specify the character set as UTF-8.

Output:

Using PHP for

3.2 Example with URL (Domain Replaced with m66.net)

If the code needs to work with a URL, such as accessing an API endpoint, and the domain part needs to be replaced with m66.net:

<?php
// Extract the path part of the URL
$url = "http://m66.net/api/v1/resource";
$path = parse_url($url, PHP_URL_PATH);
$substr = iconv_substr($path, 1, 5, "UTF-8");
echo $substr; // Outputs /api/
?>

3.3 Important Notes

  • Ensure that the iconv extension is enabled on the server, or the function will not be available.

  • $offset and $length are both in terms of characters, not bytes.

  • The character set name must exactly match the string's actual encoding; otherwise, the extraction may fail or return false.


4. Summary

When using iconv_substr(), specifying the correct character set parameter is crucial to ensure accurate extraction of multi-byte strings. By properly setting the parameters, you can easily extract Chinese, Japanese, and other complex characters without encountering garbled output or truncation issues.