troubleshooting Question

Line breaks in code?

Avatar of Derokorian
DerokorianFlag for United States of America asked on
CSSHTMLPHP
5 Comments1 Solution235 ViewsLast Modified:
I'm working on a BBCode Parser for my blog, so I can style it a little and post code snippets. It seems to be working so far, except that it's wrapping lines of the output when it shouldn't. I think its a CSS problem because the actual output (view source) is all on one line, but I'm unsure how to fix this. I have overflow set to auto, but even if i set it to  scroll it seems to not be working as intended.

Below is the code for the function and the example usage producing the erroneous output. Attached is an image showing how the output is being wrapped.

<?php

/*
 Returns the given string with BBCodes replaced by HTML and PHP tags highlighting syntax
*/
function bbmarkup($input) {
	$BBCodes = 		array('[B]'		 ,'[/B]'		 ,'[I]','[/I]','[U]','[/U]');
	$HTMLCodes = 	array('<strong>','</strong>','<i>','</i>','<u>','</u>');
	
	//*/ Find and replace URL tags
	$URLRegEx = 	 '#\[url(=(.*?))?\](.*?)\[\/url\]#i';
	$URLFind = array();
	$URLReplace = array();

	//*/ Build find and replace arrays for links
	if( preg_match_all($URLRegEx,$input,$matches) ) {
		foreach( $matches[0] as $k => $v ) {
			$URLFind[$k] = $v;
		}
		foreach( $matches[3] as $k => $v ) {
			if( empty($matches[2][$k]) )
				$url = $v;
			else
				$url = $matches[2][$k];
			$URLReplace[$k] = '<a href="'.$url.'" target="_blank">'.$v.'</a>';
		}
	}

	//*/ Find and Highlight code within PHP tags
	$PHPfind = array();
	$PHPreplace = array();
	$pattern = '#\[PHP](.+?)\[\/PHP\]#is';

	//*/ Set up replace array for PHP tags
	if( preg_match_all($pattern,$input,$matches) ) {
		foreach( $matches[1] as $k => $v ) {
			$PHPreplace[$k] = '<div class="code">'.highlight_string($v,TRUE).'</div>';
		}
	}

	//*/ Filter BBCodes and replace with HTMLcodes
	$output = str_ireplace($BBCodes,$HTMLCodes,$input);
	$output = nl2br($output);
	$output = str_replace($URLFind,$URLReplace,$output);

	//*/ Set up replace array for PHP tags (post BB filtering, so the BB tags don't get changed when IN php code)
	if( preg_match_all($pattern,$output,$matches) ) {
		foreach( $matches[0] as $k => $v ) {
			$PHPfind[$k] = $v;
		}
	}

	//*// Replace PHP tags + containing strings with highlighted version
	$output = str_replace($PHPfind,$PHPreplace,$output);
	return $output;
}

//*/ Example Usage
$str = <<<'STR'
[b]BOLD[/B]
[I]Italic[/i]
[U]Underline[/U]
Link:[url]http://www.google.com[/url]
Link with text:[url=http://www.google.com]Google![/url]

[php]<?php
class Database {
	const DBHOST = 'HOST';
	const DBUSER = 'USERNAME';
	const DBPASS = 'PASSWORD';
	const DBNAME = 'DATABASE NAME';
	
	private $lastError;
	
	private $db;
	
	public function __construct() {
		$this->db = new mysqli(self::DBHOST,self::DBUSER,self::DBPASS,self::DBNAME);
	}
	
	public function error() {
		return $this->lastError;
	}[/php]


[php] <?php
		// Build Query
		$sql = "SELECT $columns FROM `$table` WHERE ";
		foreach( $where as $k => $v ) {
			$sql .= "`$k` = '$v' AND ";
		}
		$sql = substr($sql,0,-5);
		$sql .= $limit;
		
		// Run query and return results
		$result = $this->db->query($sql);
		if( !$result ) {
			$this->lastError = 'Query Failed: ('.$this->db->errno.') '.$this->db->error.'<br>'.$sql;
			return FALSE;
		}[/php]
STR;

?>
<html>
	<head>
		<title>Highlight Test</title>
		<style type="text/css">
		div.code {
			padding: 5px;
			overflow: auto;
			width: 500px;
			max-height: 300px;
			border: 3px ridge #BBBBBB;
			background-color: #DDDDDD;
			margin: 10px 25px 10px 50px;
		}
		</style>
	</head>
	<body>
		
<?php echo bbmarkup($str); ?>

	</body>
</html><?php
//*/
Screenshot highlighting the problem
ASKER CERTIFIED SOLUTION
Derokorian

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros