note 77249 modified in function.strlen by danbrown
| From: | danbrown@php.net | Date: | Thu, 12 Feb 2009 03:13:39 +0000 |
| Subject: | note 77249 modified in function.strlen by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-150534@lists.php.net to get a copy of this message | ||
<?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
--was--
/**
* 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
http://php.net/manual/en/function.strlen.php