2

For example:

the width of "English" = 7

the width of "中文" = 4

Is there a way to get the "width" of a string?

Update:

I know that in the UTF-8 encoding,

strlen("English") = 7
strlen("中文") = 6 

mb_strlen("English") = 7
mb_strlen("中文") = 2

I want the result of some function("中文") to be 4 . You see, the width of these two strings are equal:

word
中文
riverlet
  • 369
  • 2
  • 11
  • You want string *length*. [`mb_strlen()`](https://www.php.net/manual/en/function.mb-strlen.php) and [`strlen()`](https://www.php.net/manual/en/function.strlen.php). – John Conde Feb 08 '21 at 17:54
  • `strlen()` returns the number of characters, he's looking for the width of the characters, which I doubt is possible – MrCarrot Feb 08 '21 at 17:57
  • 3
    Can you explain what "width" means? Character count, byte count, number of pixels in a specific font? – Chris Haas Feb 08 '21 at 17:59
  • 3
    What are criteria to determine that `中文`'s width is 4? – Álvaro González Feb 08 '21 at 17:59
  • 2
    Looking at the sample you just posted, on my tablet, in that font, the two strings are not equal width. – Tangentially Perpendicular Feb 08 '21 at 18:10
  • Sorry for the question (I obviously don't know anything about ideographs) but... is that visual width something fixed or it depends on the font? – Álvaro González Feb 08 '21 at 18:10
  • I don't see how those 2 strings are equal. If you mean that each character of the language you're using is equal to 2 characters of latin - then simply mb_strlen * 2. – Vitaliy-T Feb 08 '21 at 18:13
  • Does this answer your question? [How to determine the length (in pixels) of a string being rendered on a web page?](https://stackoverflow.com/questions/1641756/how-to-determine-the-length-in-pixels-of-a-string-being-rendered-on-a-web-page) – Tyzoid Feb 08 '21 at 18:21

1 Answers1

3

After browsing all the functions starting with mb_ , I found this. It is exactly what I was looking for:

mb_strwidth()

It Returns the width of a string, where halfwidth characters count as 1, and fullwidth characters count as 2.

https://www.php.net/mb_strwidth

riverlet
  • 369
  • 2
  • 11