note 77249 deleted from function.strlen by hholzgra

From: Date: Thu, 02 Aug 2012 12:15:07 +0000
Subject: note 77249 deleted from function.strlen by hholzgra
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-189615@lists.php.net to get a copy of this message
Note Submitter: mail4adry at inwind dot it ---- <?php /** * 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: <?php $text = "<p>Test 'a' paragraph.</p><!-- Comment --> Other text"; echo "strlen without HTML chars:".strlen_noHtml($text); echo "<br>"; echo "strlen of HTML chars:".strlen_html($text); ?> Will output: strlen without HTML chars:30 strlen of HTML chars:23

« previous php.notes (#189615) next »