Count HTML Tags in PHP through strlen() function
Sunday, February 28, 2010 5:51Posted in category PHP Interview Questions
No Comments
<?php
$text = "<p>This is 'a' my first paragraph.</p><!-- Comments --> sample text text";
echo "String Lenght of HTML chars:".strlen_noHtml($text); // 48 chars
function strlen_noHtml($string){
$crs = 0;
$charlen = 0;
$len = strlen($string);
while($crs < $len)
{
$offset = $crs;
$crs = strpos($string, "<", $offset);
if($crs === false)
{
$crs = $len;
$charlen += $crs - $offset;
}
else
{
$charlen += $crs - $offset;
$crs = strpos($string, ">", $crs)+1;
}
}
return $charlen;
}
?>
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.


