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

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


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


A proper (logical) alternative for unicode strings;

<?phpfunction 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" charsecho 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


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);