Here is simple code
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);
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);
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
.
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.
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;
<?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