All pastes #676266 Raw Edit

Untitled

public text v1 · immutable
#676266 ·published 2007-08-30 22:08 UTC
rendered paste body
// BBcode Parsing
//
function parseBB($string, $rawlinebreak = 0){

//REPLACE BASIC "Start And End" TAGS
//
	$basicsrc = array('/\[b\]/i','/\[\/b\]/i','/\[i\]/i','/\[\/i\]/i','/\[u\]/i','/\[\/u\]/i','/\[center\]/i','/\[\/center\]/i','/\[s\]/i','/\[\/s\]/i','/\[indent\]/i','/\[\/indent\]/i');
	$basicrpl = array('<b>','</b>','<i>','</i>','<u>','</u>','<center>','</center>','<strike>','</strike>','<p class="indent">','</p>');

	//This block will append </ending tags> for any unclosed elements.
	for($i = 0; $i < sizeof($basicsrc)-1; $i++){
		$matches_open = preg_match_all($basicsrc[$i], $string, $temp);
		$i++;
		$matches_close = preg_match_all($basicsrc[$i], $string, $temp);
		for($j = 0; $j < ($matches_open - $matches_close); $j++){
			$string .= $basicrpl[$i];
		}
	}
	
	$string = preg_replace($basicsrc, $basicrpl, $string);
	
	if(!$rawlinebreak){		
		$string = preg_replace('/\n/',"<br/>",$string);
	} else {
		$string = preg_replace('/\n/',"",$string);
	}
	
//REPLACE [tag]content[/tag]
//
	$simple_replace = array('img'=>'<img src="{{CONTENT}}" alt=""/>');	
	foreach($simple_replace as $tag=>$html){
		$markup = array();
		preg_match_all('/\['.$tag.'\](.+?)\[\/'.$tag.'\]/i', $string, $matches, PREG_PATTERN_ORDER);
		for($i = 0; $i < sizeof($matches[0]); $i++){
			$markup[] = str_replace("{{CONTENT}}",$matches[1][$i],$html);
		}
		$string = str_replace($matches[0],$markup, $string);	
	}
	
//Handle [tag=value]content[/tag]
//
	$complex_replace = array(
		'url' => '<a href="{{VALUE}}">{{CONTENT}}</a>',
		'color' => '<span style="color:{{VALUE}};">{{CONTENT}}</span>',
		'size' => '<span style="size:{{VALUE}}pt;">{{CONTENT}}</span>'
		);
	foreach($complex_replace as $tag=>$html){
		$markup = array();
		preg_match_all('/\['.$tag.'=(.+?)\](.+?)\[\/'.$tag.'\]/i', $string, $matches, PREG_PATTERN_ORDER);			
		//print_r($matches);
		for($i = 0; $i < sizeof($matches[0]); $i++){
			$tempval = str_replace("{{CONTENT}}",$matches[2][$i],$html);
			$markup[] = str_replace("{{VALUE}}",$matches[1][$i],$tempval);
		}
		$string = str_replace($matches[0],$markup, $string);
	}		
	return $string;
}