How to count touches (letters, spaces and symbols) in PHP?

Asked

Viewed 577 times

8

I made a program that extracts my annotations from my code and creates a file txt to be a little manual. I made a format to make it cooler:

+-----------+
|           |
| ESPECIAIS |
|           |
+-----------+

It takes the SPECIAL line and formats. Simple! It happens that some do not get well formatted because it has accented letters, and in this case I can not count the ringtones correctly. The line "FUNCTIONS" looks like this:

+-----------+
|           |
| FUNÇÕES |
|           |
+-----------+

This is because the string is formed by the codes 70|85|78|195|135|195|149|69|83 adding 9 characters by strlen PHP, since accents are using the prefix 195, extrapolating the count to 2.

This particular problem is not difficult to solve, but I would like to take advantage to understand the issue better. I don’t know how the encoding of accents works in this case (text file UTF-8).

In Javascript I count the touches using DOMElement.textContent.lenght which already comprises the string as text.

Is there a similar function in PHP?

1 answer

7


  • 3

    It wasn’t working. Since you answered so safely, I decided to try again. This time I forced UTF-8 in mb_strlen’s second parameter, which is optional - it worked! Sometimes I define the character set at the beginning of the php file, this time I didn’t, maybe that’s it. Anyway, it’s good the guy is attentive to this detail. Thanks!

Browser other questions tagged

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