In PHP development, when working with multibyte strings (for example, strings containing Chinese, Japanese, Korean, and other characters), standard string functions like strrpos() often fail to correctly handle character boundaries. This can lead to incorrect substring extraction or search failures. To better support multibyte character sets, PHP provides the iconv_strrpos() function. This article will explain its basic usage, parameter details, and key considerations to help you accurately find the last occurrence of a substring in your code.
iconv_strrpos() is a function from the iconv extension that finds the last occurrence of a substring within a string. Unlike strrpos(), it can more accurately identify character boundaries when dealing with multibyte encodings.
<span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-title function_ invoke__">iconv_strrpos</span></span><span> ( </span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$haystack</span></span><span> , </span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$needle</span></span><span> [, </span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$charset</span></span><span> = </span><span><span class="hljs-title function_ invoke__">ini_get</span></span><span>(</span><span><span class="hljs-string">"iconv.internal_encoding"</span></span><span>)] )
</span></span>
$haystack: The target string to search in.
$needle: The substring to find.
$charset (optional): Specifies the character set encoding of the string. If not set, it defaults to the value of iconv.internal_encoding.
Returns the position (0-based offset) of the last occurrence of $needle in $haystack. Returns false if the substring is not found.
<span><span><span class="hljs-variable">$str</span></span><span> = </span><span><span class="hljs-string">"hello world, welcome to the world of PHP"</span></span><span>;
</span><span><span class="hljs-variable">$pos</span></span><span> = </span><span><span class="hljs-title function_ invoke__">iconv_strrpos</span></span><span>(</span><span><span class="hljs-variable">$str</span></span>, </span><span><span class="hljs-string">"world"</span></span><span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-variable">$pos</span></span>; </span><span><span class="hljs-comment">// Outputs 27</span></span><span>
</span></span>
In this example, the last occurrence of the string "world" is at index 27.
<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">$needle</span></span><span> = </span><span><span class="hljs-string">"你好"</span></span><span>;
</span><span><span class="hljs-variable">$pos</span></span><span> = </span><span><span class="hljs-title function_ invoke__">iconv_strrpos</span></span><span>(</span><span><span class="hljs-variable">$str</span></span>, </span><span><span class="hljs-variable">$needle</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">$pos</span></span>; </span><span><span class="hljs-comment">// Outputs 6 (position of the second “你好” in multibyte offset)</span></span><span>
</span></span>
Note: If you use strrpos() to handle this string, it may produce incorrect results or garbled text due to character set issues. Always specify the encoding explicitly when dealing with Chinese.
Function | Multibyte Support | Recommended Use Case |
---|---|---|
strrpos() | No | Single-byte strings (e.g., ASCII) |
iconv_strrpos() | Yes | Multibyte strings (e.g., UTF-8, GBK) |
If you are working with UTF-8 encoded content (such as multilingual web pages), it is strongly recommended to use iconv_strrpos() to avoid problems caused by incorrect character boundary handling.
Specifying the character set is important: If you do not explicitly set $charset, PHP will use the default iconv.internal_encoding, which may differ from your actual string encoding, leading to errors.
Ensure the iconv extension is enabled: This function is part of the iconv extension, which is usually enabled by default with PHP, but in some custom environments, it may need to be manually enabled.
The position returned is a character offset, not a byte offset: This is crucial in multibyte encodings, otherwise you may encounter confusion during subsequent string operations.
iconv_strrpos() is a highly useful function when dealing with multibyte strings. It accurately recognizes character boundaries, helping developers find the position of substrings precisely. When working with Chinese or other non-ASCII characters, using this function correctly can greatly improve program stability and internationalization compatibility.
Whenever you are working with UTF-8 encoded content, it is advisable to use iconv_strrpos() instead of strrpos(), and always remember to specify the character set explicitly to avoid unnecessary issues.