note 77249 added to function.strlen

From: Date: Tue, 21 Aug 2007 16:28:40 +0000
Subject: note 77249 added to function.strlen
Groups: php.notes 
Request: Send a blank email to php-notes+get-129676@lists.php.net to get a copy of this message
/** * 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 = "<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 ---- Server IP: 129.16.214.50 Probable Submitter: 83.190.139.162 ---- Manual Page -- http://www.php.net/manual/en/function.strlen.php Edit -- https://master.php.net/note/edit/77249 Del: integrated -- https://master.php.net/note/delete/77249/integrated Del: useless -- https://master.php.net/note/delete/77249/useless Del: bad code -- https://master.php.net/note/delete/77249/bad+code Del: spam -- https://master.php.net/note/delete/77249/spam Del: non-english -- https://master.php.net/note/delete/77249/non-english Del: in docs -- https://master.php.net/note/delete/77249/in+docs Del: other reasons-- https://master.php.net/note/delete/77249 Reject -- https://master.php.net/note/reject/77249 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#129676) next »