Important alert: (current site time 7/15/2013 10:58:58 AM EDT)
 

VB icon

AsMDaR

Email
Submitted on: 12/8/2004 11:31:19 AM
By: Claudio Toselli  
Level: Beginner
User Rating: Unrated
Compatibility: PHP 4.0
Views: 6485
(About the author)
 
     HTMLlen counts all characters outside the HTMLs tags, so you really known how much text will be in the screen. Is usefull in dynamic site where the user input text with , and other html tags, so later you can limit that text without the risk of cut off html tags. With HTMLcut, you can limit the text to a given value, but, the chars in html are not counted as char, so if you have this a large text you user HTMLcut($str,5), you get: this And the special char like & (&) are counted as 1 char.So, in HTMLlen("this a &test"), the result will be: 12, and with strlen will be: 16. Sorry for my english, i from Argentina :)
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: AsMDaR
// Description:HTMLlen counts all characters outside the HTMLs tags, so you really known how much text will be in the screen. Is usefull in dynamic site where the user input text with <b>,<font> and other html tags, so later you can limit that text without the risk of cut off html tags.
With HTMLcut, you can limit the text to a given value, but, the chars in html are not 
counted as char, so if you have <b>this a large text</b> you user HTMLcut($str,5), you get: <b>this </b>
And the special char like & (&) are counted as 1 char.So, in HTMLlen("this a &test"), the result will be: 12, and with strlen will be: 16.
Sorry for my english, i from Argentina :)
// By: Claudio Toselli
//
// Inputs:HTMLlen need a string with text and html tags
HTMLcut need a string with text and html tags, and maximum of char to be displayed
//
// Returns:HTMLlen returns len of the strings outside of HTML tags.
HTMLcut returns the string of the size you want and ALL the HTML tags
//
// Side Effects:none, at least i think :)
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1561&lngWId=8//for details.//**************************************

function HTMLlen($str){
 $trad = get_html_translation_table (HTML_ENTITIES); // Array with special chars codes 
 $intag=false;
 $inSpecial=false;
 $strSpecial='';
 $cont=0;
 $contchar=0; // Counts chars outside tags
 while ($cont<strlen($str)){
$char=substr($str,$cont,1); // Get the char in cont position
if (!$intag){ // It's not in a tag
if ($char=='<'){ // Check if start tag
	$intag=true;
	 }else{
		 if (!$inSpecial) { // Check if i in the middle of a special char
		if ($char!='&'){ // Check if no special char is begining
		 $contchar++;
		}else{
			$inSpecial=true;
				 $strSpecial=$strSpecial.$char; // Is a special char, i put in a buffer for later processing
			 }
		 }else{
 	 $strSpecial=$strSpecial.$char; // Is a special char, i put in a buffer for later processing
		if ($char==';'){ // Check if it's the final of the special char
			if (in_array($strSpecial,$trad)){ // Check if is a valid special char
				 $contchar++;
				 }else{
				 $contchar=$contchar+strlen($strSpecial);
				 }
				 $strSpecial='';
				 $inSpecial=false;
			 }
		 }
		}
	 }else{
if ($char=='>'){ // End tag
	$intag=false;
	 }
	 }
$cont++;
 }
 $contchar=$contchar+strlen($strSpecial); // If special char was started but never ended i add the length of what i have in the buffer
 return $contchar;
}
function HTMLCut($str,$tope){
 $trad = get_html_translation_table (HTML_ENTITIES);
 $intag=false;
 $inSpecial=false;
 $strSpecial='';
 $cont=0;
 $contchar=0; // Counts char outside tags
 $retStr=''; // Buffer for the string smaller
 while ($cont<strlen($str)){
$char=substr($str,$cont,1);
if (!$intag){ // It's not in a tag
if ($char=='<'){ // Check if start tag
 // A tag begining was detected, i must cancel the search for a special char because it's imposible
 // so i put the content of special char in buffer in the returning string and i add the lenght of 
 // the buffer to contchar so i know if i get to tope (in case i never start to search a special char,
 // does nothing, just start the tag)
 if ($inSpecial){
$retStr=$retStr.substr($strSpecial,0,$tope-$contchar);
 	 $contchar=$contchar+strlen($strSpecial);
 	 $strSpecial='';
 	 $inSpecial=false;
		 }
 $retStr=$retStr.$char;
	$intag=true;
	 }else{
		 if ($contchar<$tope){ // Check if i reach the max allowed
	 if (!$inSpecial) { // Check if i in the middle of a special char
		 if ($char!='&'){ // Check if no special char is begining
		 $retStr=$retStr.$char; // Add char to buffer
		 $contchar++;
		 }else{
			$inSpecial=true;
				 $strSpecial=$strSpecial.$char;
			}
		}else{
 	$strSpecial=$strSpecial.$char;
		 if ($char==';'){ // Check if it's the final of the special char
			if (in_array($strSpecial,$trad)){ // Check if is a valid special char
		 		$contchar++;
				 }else{
 			 	$contchar=$contchar+strlen($strSpecial);
				 }
				 $retStr=$retStr.$strSpecial;
				 $strSpecial='';
					$inSpecial=false;
			}
		}
		 }
		}
	 }else{
$retStr=$retStr.$char; // Add char to buffer
if ($char=='>'){ // End tag
	$intag=false;
	 }
	 }
//echo "-->".htmlentities($retStr)."<--->".htmlentities($strSpecial)."<--<br>";
$cont++;
 }
 if ($contchar<$tope && $strSpecial!=''){
// If i have something in the buffer i put it in the returning string,
// only the amount of chars to reach the limit
$retStr=$retStr.substr($strSpecial,0,$tope-$contchar);
 }
 return $retStr;
} ?>


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.