PHP Function Iconv_Substr
php function iconv_substr
The php function iconv_substr() is an inbuilt function in PHP that lets you extract a portion of string based on the specified character encoding. It takes four parameters that are described below: $string: The input string from which you want to extract a portion. $start: The position from where the first extraction will begin in the string. $length: The number of characters you want to extract from the string.
$encoding: The character encoding that the string was encoded in. If this parameter is omitted or null, the string will be assumed to be encoded in iconv.internal_encoding.
Note: In PHP versions prior to 8.0, substr and grapheme_substr functions would throw ValueError exceptions on negative string offsets, but since PHP 8.0 they have clamped this and now return an empty string instead. This is a change that was also implemented for the iconv_substr function and brings it in line with mb_substr, which has always returned an empty string on negative offsets.
When the string is encoded in a different encoding than its target encoding, you can use this function to remove all the illegal characters from the output of the string. Then you can use mb_substr to replace these with the correct characters. This is an alternative to re-encoding the entire string with the target encoding. It is much faster than re-encoding and is also much safer to use, as it ensures that all the characters are properly converted and that you don't end up with any invalid characters in the final output string.