Jump to content

Creating my own code boxes: extra linebreaks?


MishieMoo

Recommended Posts

Okay so I've been writing some tutorials, and I've run into a little snag.  I wrote a little parser in order to be able to display the code on the page.  Everything is stored in a database, so doing it by hand just isn't possible.  Here's what I wrote:

<?php function format_text($text){
		 $text=stripslashes($text);
		 $new='<div class="code"><xmp>$1</xmp></div>';
		 $old='/\[code\](.*?)\[\/code\]/is';
		 $text=preg_replace($old, $new, $text);
		 $text=nl2br($text);
		 return $text;
		 } ?>

 

And it works pretty well, except in the <xmp>'s.  There I have a bunch of extra <br />'s at the end of each line of code that I simply don't want.  I can't seem to find anyplace that has a tutorial or anything on this, so some help would be appreciated.  Is there some way to select where the <br />'s are placed?

 

Thanks in advance ^_^+

 

post ur format off text example

<?php

$text='<?php echo redarrow loves php <br> hi everyone; ?>';

function format_text($text){
		 $text=stripslashes($text);
		 $new='<div class="code"><xmp>$1</xmp></div>';
		 $old='/\[code\](.*?)\[\/code\]/is';
		 $text=preg_replace($old, $new, $text);
		 $text=nl2br($text);
		 return $text;
		 }

		 echo format_text($text);
		 ?>

try this
<?php
function format_text($text){
		 $text=stripslashes($text);
		 $new='<div class="code"><xmp>$1</xmp></div>';
		 $old='/\[code\](.*?)\[\/code\]/is';
		 $text=preg_replace($old, $new, $text);
		 $text=nl2br($text);
		 $text=preg_replace("/'s/",'',$text);
		 return $text;
		 }

		 echo format_text($text);
		 ?>

That didn't do anything.  There are still a bunch of extra linebreaks all over the place.  Here's a sample in case you don't understand what's happening.

 

$width=$swidtharr[$size]*$digits;<br />
$image=ImageCreate($width,$height);<br />
$background=ImageColorAllocate($image, $bgr, $bgg, $bgb);<br />
$text=ImageColorAllocate($image, $txr, $txg, $txb);<br />

 

And those linebreaks aren't supposed to be there.  But I can't take out the nl2br function or use anything else because the entire tutorial comes from a database, so the regular text part still needs that function.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.