I can not limit text with Japanese characters

3

I'm trying to limit the text "in Japanese" that will be displayed in some places of the page, it does not seem like php can handle this kind of character well.

Example php :

  

echo substr ("あ え い う お", 0, 3);

Here it shows only "あ" and should show "あ え い", that is 3 characters.

Then I tried with css:

  

style="overflow: hidden; width: 105px;"

In this case it simply ignores and does not limit anything, where all text with Japanese characters goes to the screen.

    
asked by anonymous 14.03.2016 / 08:59

1 answer

5
string functions

You are using the wrong function. The substr function is for use with encodings of 1 per character, such as ISO-8859-1.

The functions for multibyte have the prefix mb_ .

The correct one:

echo mb_substr("あえいうお", 0, 3);

See working at IDEONE .


The same thing goes for mb_strlen , and most other string functions.


About overflow

If you do not avoid the line break, hidden does not solve itself.

See an example that works:

Com quebra de linha:
<div style="overflow: hidden; width:105px; border:1px solid red;">
それは隠されていました</div>
Sem quebra de linha:
<div style="overflow: hidden; width:105px; border:1px solid red; white-space:nowrap;">
それは隠されていました</div>
    
14.03.2016 / 09:54