Burak Keskin
Burak Keskin

Reputation: 125

Php character count without spaces and html tags

I want to count characters in a text like Microsoft Word.

<p><b>Lorem Ipsum</b> is simply dummy text of the printing and typesetting industry.Tom's farm. 12th century.</p>

12th -> 'th' is a special character, under the number 12.

How can I do that in PHP?

Upvotes: 0

Views: 3484

Answers (2)

Burak Keskin
Burak Keskin

Reputation: 125

My solution is;

    $content = 'Some Text...';

    $trim = strip_tags($content);
    $trim=str_replace([" ","\n","\t","&ndash;","&rsquo;","&#39;","&quot;","&nbsp;"], '', $trim);

    $totalCharacter = strlen(utf8_decode($trim));

Upvotes: 1

alexandre
alexandre

Reputation: 335

$string = "<p><b>Lorem Ipsum</b> is simply dummy text of the printing and typesetting industry.Tom's farm. 12th century.</p>";
$string = strip_tags($string);
$string = preg_replace("/\s/", "", $string);
$character_count = strlen($string);

If there is a th Unicode character, strlen will probably count it as two characters. Then you should maybe have a look on mb_strlen.

Upvotes: 0

Related Questions