I would like to count the length of a string with PHP. The string contains HTML entity numbers, which inflate the number of characters that are counted: a dash is –
which is counted as 7 when I only want it to count as 1.
How do I convert the html numbered entities to a form where special characters are only counted with a length of 1?
Example string:
Goth-Trad – ‘Cosmos’
The code:
$string = html_entity_decode('Goth-Trad – ‘Cosmos’');
echo strlen($string);
produces '38', when I'm looking for '20'. What is going wrong?
You can use this:
$html = 'Goth-Trad – ‘Cosmos’';
echo strlen(utf8_decode(html_entity_decode($html, ENT_COMPAT, 'utf-8')));
Just decode it and count the decoded one?
$string = html_entity_decode("Goth-Trad – ‘Cosmos’",ENT_QUOTES,"UTF-8");
echo strlen($string);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With