JVerstry JVerstry - 1 month ago 18
PHP Question

Substring results in non-printable characters

As part of inserting product names in a flat file, I have to truncate them to 50 characters at most.

However, some product names contain characters with accents and if these are located at the 50th position (or so), the truncated product name does not display properly.

Here is how to replicate the issue (PHP):

$tmp = "Kaspersky Anti-Virus 2015 (Renovación) Suscripción Anual 3 PC";
$xxx = substr(trim($tmp), 0, 50);

echo $tmp . PHP_EOL;
echo $xxx . PHP_EOL;


displays:

Kaspersky Anti-Virus 2015 (Renovación) Suscripción Anual 3 PC
Kaspersky Anti-Virus 2015 (Renovación) Suscripci�


How can I get rid of these undisplayable characters �?

Answer

You need to use mb_substr instead of substr:

$tmp = "Kaspersky Anti-Virus 2015 (Renovación) Suscripción Anual 3 PC";
$xxx = mb_substr(trim($tmp), 0, 50);

echo $tmp . PHP_EOL;
echo $xxx . PHP_EOL;

From the docs:

Performs a multi-byte safe substr() operation based on number of characters. Position is counted from the beginning of str. First character's position is 0. Second character position is 1, and so on.

Comments