When I use the substr() function in PHP, I get an question mark (a square with a question mark - depending on the browser) at the end of the string when this last character was a special one, like ë or ö, etc...
$introtext = html_entity_decode($item->description, ENT_QUOTES, "UTF-8");
$introtext = substr($introtext, 0, 200);
How can I escape this?
substr in PHP is a built-in function used to extract a part of the given string. The function returns the substring specified by the start and length parameter. It is supported by PHP 4 and above. Let us see how we can use substr() to cut a portion of the string.
The SUBSTR function acts on a character string expression or a bit string expression. The type of the result is a VARCHAR in the first case and VARCHAR FOR BIT DATA in the second case. The length of the result is the maximum length of the source type.
The SUBSTR function returns a portion of string, beginning at a specified character position, and a specified number of characters long. SUBSTR calculates lengths using characters as defined by the input character set.
MySQL SUBSTR() Function The SUBSTR() function extracts a substring from a string (starting at any position).
If your string has multibyte encoding (like UTF-8) does, you should use mb_substr
to avoid problems like this:
$introtext=mb_substr($introtext,0,200);
In case someone tried the previous answers, and it still didn't work:
Try to add a Unicode name in mb_substr
like:
$introtext = mb_substr($introtext, 0, 200, 'utf-8');
Use mb_substr
That is because substr does not work with multibyte characters. substr will probably cut a multibyte character "in half". You should instead use mb_substr. Also make sure that your file is saved in UTF-8.
$introtext = mb_substr($introtext, 0, 200);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With