Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using PHP's substr() with special characters at the end results in question marks

Tags:

php

substr

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?

like image 786
Bert Avatar asked Jun 06 '11 20:06

Bert


People also ask

What is substr () in PHP and how it is used?

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.

What is the purpose of substr () function?

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.

What is the return type of the substr () function?

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.

Which Substr function is used to extract characters?

MySQL SUBSTR() Function The SUBSTR() function extracts a substring from a string (starting at any position).


4 Answers

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);
like image 103
lonesomeday Avatar answered Oct 06 '22 11:10

lonesomeday


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');
like image 25
Sruit A.Suk Avatar answered Oct 06 '22 11:10

Sruit A.Suk


Use mb_substr

like image 27
ssapkota Avatar answered Oct 06 '22 10:10

ssapkota


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);
like image 5
alexn Avatar answered Oct 06 '22 12:10

alexn