Use iconv_strrpos() when you need to finds the last occurrence of a substring in a given character encoding in a PHP application. The examples below show the actual operation and its limitations.
What Is iconv_strrpos() in PHP?
Finds the last occurrence of a substring in a given character encoding. The exact signature supported by your installation depends on its PHP version and enabled extensions.
Its syntax is:
iconv_strrpos(string $haystack, string $needle, ?string $encoding = ...): int|false
Parameters and return value: The parameter types, optional arguments, references, and return type are shown in the signature above. Optional parameters are marked with an ellipsis in this reference signature.
Example 1: Basic Usage
<?php
echo iconv_strrpos("café café","café","UTF-8");
Output:
5
Example 2: Different Input or Behavior
<?php
echo iconv_strrpos("red blue red","red","UTF-8");
Output:
9
Practical Example: Applying the Function
<?php
$sentence = "été and été"; $position = iconv_strrpos($sentence,"été","UTF-8"); var_dump($position);
Output:
int(8)
Important Behavior and Edge Cases
Search results are character offsets; always check === false because position zero is valid.
Comparison With Related Functions
| Approach | Difference |
|---|---|
iconv_strrpos() | Finds the last occurrence of a substring in a given character encoding. |
| Related operation | strrpos() uses byte offsets; iconv_strrpos() is encoding-aware. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring the function-specific limitation | Search results are character offsets; always check === false because position zero is valid. |
| Confusing this function with a related operation | strrpos() uses byte offsets; iconv_strrpos() is encoding-aware. |
Key Takeaways
- Finds the last occurrence of a substring in a given character encoding.
- Search results are character offsets; always check === false because position zero is valid.
- strrpos() uses byte offsets; iconv_strrpos() is encoding-aware.
Use iconv_strrpos() for the specific operation demonstrated above. Check its prerequisites and handle failure conditions before using it with production data.