Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting length of string with HTML numbered entities in PHP

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?

like image 712
Squrler Avatar asked Jan 02 '12 13:01

Squrler


2 Answers

You can use this:

$html = 'Goth-Trad – ‘Cosmos’';
echo strlen(utf8_decode(html_entity_decode($html, ENT_COMPAT, 'utf-8')));
like image 57
Peter Krejci Avatar answered Sep 19 '22 00:09

Peter Krejci


Just decode it and count the decoded one?

$string = html_entity_decode("Goth-Trad – ‘Cosmos’",ENT_QUOTES,"UTF-8");
echo strlen($string);
like image 26
Damien Pirsy Avatar answered Sep 23 '22 00:09

Damien Pirsy