Link to home
Start Free TrialLog in
Avatar of Derokorian
DerokorianFlag for United States of America

asked on

Line breaks in code?

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
//*/

Open in new window

User generated image
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Not sure I am understanding the question completely, but you might find some good results with this function.
http://php.net/manual/en/function.nl2br.php
Avatar of Derokorian

ASKER

The problem is the output has one line of php "wrapping" to the next line, so one line of input (php code) becomes 2 lines of output it seems to always wrap at a dash which is going to be used a lot for my Object Oriented snippets. I can't have all my OO code not displaying properly.
In my original post the code block is the entire script i'm using (css styles, function definition, input definition and call to function for output). You can feel free to copy paste it to run on your own server.
ASKER CERTIFIED SOLUTION
Avatar of Derokorian
Derokorian
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for your time, found a solution on a forum.