Displaying code in a web page

Hi,

I have the following function that I use to display code on my page.

function filter_code($text) {
	if (preg_match_all('/\\[CODE\\](.*)\\[\\/CODE]/msU', $text, $callbacks)) {
		$replace = $callbacks[0];
		$with = preg_replace('/ /', ' ', $callbacks[1]);
		foreach ($callbacks[1] as $k => $v) {
			$text = str_replace($replace[$k], htmlspecialchars($with[$k]), $text);
			$text = preg_replace('/ /', ' ', $text);
		}
		$removeTags = array('

', ’

');
		$text = str_replace($removeTags, '', $text);
	}
	return $text;
}

For example this function gets the following code piece in the post body


<!DOCTYPE html>
<html>
<head>
<title>My Web Page</title>
</head>
<body>
<p>Welcome to my web page!</p>
</body>
</html>


and displays it on the page as code. With one issue though, it does not take new lines into account and displays it like this:

<!DOCTYPE html> <html> <head>   <title>My Web Page</title> </head> <body>   <p>Welcome to my web page!</p> </body> </html>

How can I modify it so that it will display with new lines as shown above? Thanks for any ideas.

Have you tried highlight_file() or highlight_string()?

Poked from a tablet :frowning:

I’m not sure if this will work for you, but you could try using a <pre> tag: http://www.w3schools.com/tags/tag_pre.asp

Tried after you mentioned but didn’t work.

<pre> tag preformats the text, it doesn’t display html code on the page. Thanks, though.

Maybe use PHP heredoc and HTML <pre> :


<?php 
$code=<<<CODE
<!DOCTYPE html>
<html>
<head>
  <title>My Web Page</title>
</head>
<body>
  <p>Welcome to my web page!</p>
</body>
</html>
CODE;


$style='float:left; background-color:lightgreen; color:black; padding: 0.42em; outline:dotted: 2px #f00';
echo '<pre style="'.$style .'">';
  echo  htmlspecialchars($code ) ;
echo '</pre>';
?>



Output:

<!DOCTYPE html>
<html>
<head>
<title>My Web Page</title>
</head>
<body>
<p>Welcome to my web page!</p>
</body>
</html>

Beware of trailing spaces when using heredoc :slight_smile:

Have you tried highlight_file() or highlight_string()?

I use both the above on my website without a problem; I would guess you are removing some important character in your function.

I recommend starting your function from fresh using highlight_string() and the absolute basic code and build up from there.

John, thank you but I can’t use that because the post is stored in database. I can’t have PHP code in it.

the post is stored in database

Is the data stored in the database correctly - check with phpmyadmin.

I think so. Why did you ask that? Can you store PHP code in database? Also if you can give a working example for highlight_string() that displays HTML code on the screen, it will be much appreciated.

Thank you.

If the text is stored in a database then try this:



echo nl2br( $text );

// or

echo nl2br( htmlspecialchars($text ) );
 

Well, I had that but it adds a new line to whole post body. I just want to add a new line between CODE tags in my post body.

<p>This is my post body</p>

<p>This is sample code</p>
<div>Some more code</div>


<p>This is some more text</p>

Like in a way the function replaces empty spaces like this:

$with = preg_replace('/ /', '&nbsp;', $callbacks[1]);