I cannot limit text with Japanese characters

Asked

Viewed 245 times

4

I’m trying to limit the text "in Japanese" that will be displayed in some places on the page, it doesn’t seem that php can handle this type of character well.

Example of php:

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

Here shows only "あ" and, should show "あ え い", ie 3 characters.

So 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.

1 answer

6


Functions of string

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

The functions to multibyte has the prefix mb_.

Right:

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

See working on IDEONE.


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


About the overflow

If you do not avoid line breaking, the Hidden in itself does not solve.

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>

  • nossaaa! was worth a lot! as I searched this and did not find it!! briogadao!

  • Alias, this thing that put a code together, as an example and working is simply too... people like Noob have a lot of difficulty.... thank you for being clear and objective!

  • 1

    We don’t always get it, but we’re there for it ;)

  • Continuing with the topic, I would like to know if invez de 105px, I can use a amount of characters, for example 10 characters?

  • gave sequence to the question here in the following topic: http://answall.com/questions/118183/comor-limitar-display-de-texto-em-uma-div-por-qtd-characters

  • 1

    I think it’s too similar to be a new question. Anyway, you can’t do this with CSS alone. What you can do is a mix of CSS and PHP, to hide a part, but show when clicked. And already have this answered here on the site. http://answall.com/questions/110953/como-mostrar-somente-x-caracteres-em-uma-div/110958?s=3|0.0415#110958

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.