Return to Snippet

Revision: 12614
at April 17, 2009 23:15 by kossmoss


Updated Code
function closetags($text){
	preg_match_all("#<([a-zA-Z]{1,50})[^>]*>#i",$text,$otags);
		if(count($otags[1])>0){
			$fotags=array();
			$fctags=array();
			foreach($otags[1] as $otag){
				$otag = strtolower($otag);
				if(isset($fotags[$otag]))
					$fotags[$otag]++;
				else
					$fotags[$otag] = 1;
			}
			preg_match_all("#</([a-zA-Z]{1,50})#i",$text,$ctags);
			foreach($ctags[1] as $ctag){
				$ctag = strtolower($ctag);
				if(isset($fctags[$ctag]))
					$fctags[$ctag]++;
				else
					$fctags[$ctag] = 1;
			}
			$rtags = array_reverse($fotags, TRUE);
			foreach ( $rtags as $tag => $cnt)
			{
				$fctags[$tag] = isset($fctags[$tag]) ? $fctags[$tag] : 0;
				$text .= str_repeat("</".$tag.">",abs($fctags[$tag] - $cnt));
			}
		}
	$wrong_replace = array(
		'</br>' => '',
		'</img>' => '',
		'</hr>' => '',
		'</link>' => '',
		'</meta>' => ''
		);
	$text=strtr($text, $wrong_replace);	
	return $text;
}

Revision: 12613
at April 17, 2009 22:31 by kossmoss


Updated Code
function closetags($text){
	preg_match_all("#<[^a-zA-Z>/]*([a-zA-Z]{1,50})#i",$text,$otags);
		if(count($otags[1])>0){
			$fotags=array();
			$fctags=array();
			foreach($otags[1] as $otag){
				$otag = strtolower($otag);
				if(isset($fotags[$otag]))
					$fotags[$otag]++;
				else
					$fotags[$otag] = 1;
			}
			preg_match_all("#</([a-zA-Z]{1,50})#i",$text,$ctags);
			foreach($ctags[1] as $ctag){
				$ctag = strtolower($ctag);
				if(isset($fctags[$ctag]))
					$fctags[$ctag]++;
				else
					$fctags[$ctag] = 1;
			}
			foreach ( $fotags as $tag => $cnt)
			{
				$fctags[$tag] = isset($fctags[$tag]) ? $fctags[$tag] : 0;
				$text .= str_repeat("</".$tag.">",abs($fctags[$tag] - $cnt));
			}
		}
	return $text;
}

Revision: 12612
at March 30, 2009 23:34 by kossmoss


Updated Code
function closetags($text){
		//last word cutting
		$text = substr($text, 0, strrpos($text," "));	
		$text = preg_replace("/<[^>]*$/i", "", $text);
		//tag closing
		preg_match_all("/<[^a-zA-Z>\/]*([a-zA-Z]{1,50})/i",$text,$otags);
			if(count($otags[0])>0){
				$fotags=array();
				$fctags=array();
				preg_match_all("/<[ t]*\/[^a-z]*([a-z]{1,50})/i",$text,$ctags);
				foreach($otags[1] as $otag){
					$otag = strtolower($otag);
					if(isset($fotags[$otag]))
						$fotags[$otag]++;
					else
						$fotags[$otag] = 1;
				}
				foreach($ctags[1] as $ctag){
					$ctag = strtolower($ctag);
					if(isset($fctags[$ctag]))
						$fctags[$ctag]++;
					else
						$fctags[$ctag] = 1;
				}
				while(list($tag, $cnt) = each($fotags)){
					$fctags[$tag] = isset($fctags[$tag]) ? $fctags[$tag] : 0;
					$text .= str_repeat("",abs($fctags[$tag] - $cnt));
				}
			}
		return $text;
	}

Revision: 12611
at March 20, 2009 20:59 by kossmoss


Initial Code
function closetags($text){
		//обрезка последнего слова
		$text = substr($text, 0, strrpos($text," "));	
		$text = preg_replace("/<[^>]*$/i", "", $text);
		//закрытие тегов
		preg_match_all("/<[^a-zA-Z>\/]*([a-zA-Z]{1,50})/i",$text,$otags);
			if(count($otags[0])>0){
				$fotags=array();
				$fctags=array();
				preg_match_all("/<[ t]*\/[^a-z]*([a-z]{1,50})/i",$text,$ctags);
				foreach($otags[1] as $otag){
					$otag = strtolower($otag);
					if(isset($fotags[$otag]))
						$fotags[$otag]++;
					else
						$fotags[$otag] = 1;
				}
				foreach($ctags[1] as $ctag){
					$ctag = strtolower($ctag);
					if(isset($fctags[$ctag]))
						$fctags[$ctag]++;
					else
						$fctags[$ctag] = 1;
				}
				while(list($tag, $cnt) = each($fotags)){
					$fctags[$tag] = isset($fctags[$tag]) ? $fctags[$tag] : 0;
					$text .= str_repeat("",abs($fctags[$tag] - $cnt));
				}
			}
		return $text;
	}

Initial URL
http://www.promoforum.ru/index.php?showtopic=12687&view=findpost&p=97701

Initial Description
closes opened tags in text piece.
original code slightly modified to supress PHP warnings

NOTE: need to fix the order of closing tags to get standards compliance

Initial Title
Tag closing for HTML text pieces

Initial Tags
html, text, function

Initial Language
PHP