How can I accurately calculate the number of characters in a string with special characters using PHP?

I am using strlen

to calculate the length of a string, and if it exceeds x characters, substr

it is with ellipses. The problem is, if there are special characters, the function strlen

spits out an inaccurate number, causing ellipses to be appended to lines that are no longer than x characters. Which is a good method of calculating the length of a string in a way that even works with special characters, preferably assuming the special character is a regular character, so something like "W ^ a" counts as three characters.


source to share

1 answer

If by special characters

you mean something like öüóűőúéáí than you can try the mb_strlen method passing "utf-8" as the second parameter.



All Articles