21
4

3 回答 3

21

See below URL:

Extracting a substring from a UTF-8 string in PHP

http://osc.co.cr/extracting-a-substring-from-a-utf-8-string-in-php/

PHP substring with UTF-8

http://greekgeekz.blogspot.in/2010/11/php-substring-with-utf-8.html

Or try it:

Example #1

$str1 = utf8_encode("Feliz día");

$str2 = substr($str1, 0, 9);

echo utf8_decode($str2); 

// will output Feliz d�

Example #2

$str3 = mb_substr($str1, 0, 9, 'UTF-8');

echo utf8_decode($str3); 

// will output Feliz dí

As of PHP >= 5.3 you can also declare the encoding directive and use the substr function

Example #3

declare(encoding='UTF-8');

$str4 = "Feliz día";

$str5 = substr($str4, 0, 9);echo $str5;


// will output Feliz dí
于 2012-09-16T12:52:09.407 回答
8

As usual, the answer appears to have been here. (Honestly, I have searched for about an hour)

An answer at (鉑) string functions and UTF8 in php reads:

Make sure you set the proper internal encoding: mb_internal_encoding('utf-8');

With this mb_internal_encoding('utf-8'); everything works fine. Sorry to bother you guys, thanks for help.

于 2012-09-16T12:48:06.147 回答
7

Try mb_strcut().
Its behavior is same to substr(), except it doesn't leave the last character to be broken.
If at the position you are trying to cut out, have a multibyte character with 2 or more bytes, mb_strcut() will not cut the character into pieces, but will ignore this character.

For instance, if your are trying to cut out 50 bytes out of the string Лампа в вытяжке на кухне меняется, начиная с вытаскивания белого штырька справа., mb_strcut() will not cut the character н in half, but will eliminate it from the result.

$str = "Лампа в вытяжке на кухне меняется, начиная с вытаскивания белого штырька справа.";

echo mb_strcut($str, 0, 50);
// Prints: Лампа в вытяжке на кухне ме

echo substr($str, 0, 50);
// Prints: Лампа в вытяжке на кухне ме�

echo mb_substr($str, 0, 50);
// Prints: Лампа в вытяжке на кухне меняется, начиная с вытас

Hope it helps.

于 2016-06-09T14:02:40.413 回答