php substr() function with utf-8 leaves � marks at the end

PhpUtf 8Substr

Php Problem Overview


Here is simple code

<?php

$var = "Бензин Офиси А.С. также производит все типы жира и смазок и их побочных        продуктов в его смесительных установках нефти машинного масла в Деринце, Измите, Алиага и Измире. У Компании есть 3 885 станций технического обслуживания, включая сжиженный газ (ЛПГ) станции под фирменным знаком Петрогаз, приблизительно 5 000 дилеров, двух смазочных смесительных установок, 12 терминалов, и 26 единиц поставки аэропорта.";

$foo = substr($var,0,142);

echo $foo;
?>

and it outputs something like this:

Бензин Офиси А.С. также производит все типы жира и смазок и их побочных продук�...

I tried mb_substr() with no luck. How to do this the right way?

Php Solutions


Solution 1 - Php

The comments above are correct so long as you have mbstring enabled on your server.

$var = "Бензин Офиси А.С. также производит все типы жира и смазок и их побочных        продуктов в его смесительных установках нефти машинного масла в Деринце, Измите, Алиага и Измире. У Компании есть 3 885 станций технического обслуживания, включая сжиженный газ (ЛПГ) станции под фирменным знаком Петрогаз, приблизительно 5 000 дилеров, двух смазочных смесительных установок, 12 терминалов, и 26 единиц поставки аэропорта.";

$foo = mb_substr($var,0,142, "utf-8");

Here's the php docs:

http://php.net/manual/en/book.mbstring.php

Solution 2 - Php

A proper (logical) alternative for unicode strings;

<?php
function substr_unicode($str, $s, $l = null) {
    return join("", array_slice(
        preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY), $s, $l));
}

$str = "Büyük";
$s = 0; // start from "0" (nth) char
$l = 3; // get "3" chars
echo substr($str, $s, $l) ."\n";    // Bü
echo mb_substr($str, $s, $l) ."\n"; // Bü
echo substr_unicode($str, $s, $l);  // Büy
?>

Use the PHP: mb_substr - Manual

Solution 3 - Php

If your strings may contain Unicode (multi-byte) characters and you don’t want to break these, replace substr with one of the following two, depending on what you want:

Limit to 142 characters:

mb_substr($var, 0, 142);

Limit to 142 bytes:

mb_strcut($var, 0, 142);

Solution 4 - Php

PHP5 does not understand UTF-8 natively. It is proposed for PHP6, if it ever comes out.

Use the multibyte string functions to manipulate UTF-8 strings safely.

For instance, mb_substr() in your case.

Solution 5 - Php

Never use constant in substr function for UTF-8 string:

$st = substr($text, $beg, 100);

50% chance you will get half of a character at end of the string.

Do it like this:

$postion_degin = strpos($text, $first_symbol);
$postion_end = strpos($text, $last_symbol);
$len = $postion_end - $postion_degin + 1;
$st = substr($text, $postion_degin, $len);

100% safe result.

No mb_substr.

Solution 6 - Php

If you want to use strlen function, to calculate length of string, which you want to return and your string $word has UTF-8 encoding, you have to use mb_strlen() function:

$foo = mb_substr($word, 0, mb_strlen($word)-1);

Solution 7 - Php

I hope this solution help you as it helped me a lot.

<?php
if(mb_strlen($post->post_content,'UTF-8')>200){
    $content= str_replace('\n', '', mb_substr(strip_tags($post-> post_content), 
                          0, 200,'UTF-8'));
    echo $content.'…';
}else{
    echo str_replace('\n', '', strip_tags($post->post_content));
}
?>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNazarView Question on Stackoverflow
Solution 1 - PhpKai QingView Answer on Stackoverflow
Solution 2 - PhpBotir ZiyatovView Answer on Stackoverflow
Solution 3 - PhpcawView Answer on Stackoverflow
Solution 4 - PhpthwdView Answer on Stackoverflow
Solution 5 - PhpusergioView Answer on Stackoverflow
Solution 6 - PhpGuga NemsitsveridzeView Answer on Stackoverflow
Solution 7 - PhpJodyshopView Answer on Stackoverflow