$text = 'hello world';
echo strlen($text); // 11
?>
/**
* return length of a string regardeless of html tags in it
*
* @param string $html
* @return string
*/
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;
}
/**
* return length of a string regarding html tags in it
*
* @param string $html
* @return string
*/
function strlen_Html($string){
$crs = 0;
$charlen = 0;
$len = strlen($string);
while($crs < $len)
{
$scrs = strpos($string, "<", $crs);
if($scrs === false)
{
$crs = $len;
}
else
{
$crs = strpos($string, ">", $scrs)+1;
if($crs === false)
$crs = $len;
$charlen += $crs - $scrs;
}
}
return $charlen;
}
?>
Example:
$text = "Test 'a' paragraph.
Other text";
echo "strlen without HTML chars:".strlen_noHtml($text);
echo "
";
echo "strlen of HTML chars:".strlen_html($text);
?>
Will output:
strlen without HTML chars:30
strlen of HTML chars:23 Edit