Reputation: 125
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
Reputation: 125
My solution is;
$content = 'Some Text...';
$trim = strip_tags($content);
$trim=str_replace([" ","\n","\t","–","’","'","""," "], '', $trim);
$totalCharacter = strlen(utf8_decode($trim));
Upvotes: 1
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