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

5

I made a program that extracts my notes from my code and creates a txt file to be a little manual. I formatted to stay cool:

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

It takes the SPECIAL line and formats it. Simple! It happens that some are not well formatted because there are letters with an accent, and in this case I can not count the tones correctly. The line "FUNCTIONS" looks like this:

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

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

This particular problem is not difficult to solve, but I want to take this opportunity to better understand the issue. I do not know how encoding works in this case (UTF-8 text file).

In JavaScript I count the strokes using DOMElement.textContent.lenght which already understands the string as text.

Is there a similar function in PHP?

    
asked by anonymous 26.03.2015 / 19:22

1 answer

5

The simplest solution is to use the mb_strlen() function that is prepared to handle multibyte characters such as UTF-8.

mb_strlen("FUNÇÕES") // produz 7

See running on ideone .

    
26.03.2015 / 19:28