MishieMoo Posted May 26, 2008 Share Posted May 26, 2008 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 + Link to comment https://forums.phpfreaks.com/topic/107250-creating-my-own-code-boxes-extra-linebreaks/ Share on other sites More sharing options...
redarrow Posted May 26, 2008 Share Posted May 26, 2008 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); ?> Link to comment https://forums.phpfreaks.com/topic/107250-creating-my-own-code-boxes-extra-linebreaks/#findComment-549908 Share on other sites More sharing options...
MishieMoo Posted May 26, 2008 Author Share Posted May 26, 2008 That doesn't make any difference and doesn't help. Link to comment https://forums.phpfreaks.com/topic/107250-creating-my-own-code-boxes-extra-linebreaks/#findComment-549912 Share on other sites More sharing options...
redarrow Posted May 26, 2008 Share Posted May 26, 2008 use str_replace() Link to comment https://forums.phpfreaks.com/topic/107250-creating-my-own-code-boxes-extra-linebreaks/#findComment-549974 Share on other sites More sharing options...
redarrow Posted May 26, 2008 Share Posted May 26, 2008 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); ?> Link to comment https://forums.phpfreaks.com/topic/107250-creating-my-own-code-boxes-extra-linebreaks/#findComment-549979 Share on other sites More sharing options...
MishieMoo Posted May 26, 2008 Author Share Posted May 26, 2008 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. Link to comment https://forums.phpfreaks.com/topic/107250-creating-my-own-code-boxes-extra-linebreaks/#findComment-550190 Share on other sites More sharing options...
MishieMoo Posted May 26, 2008 Author Share Posted May 26, 2008 ...bump. I don't know how to fix this xD Link to comment https://forums.phpfreaks.com/topic/107250-creating-my-own-code-boxes-extra-linebreaks/#findComment-550402 Share on other sites More sharing options...
BlueSkyIS Posted May 26, 2008 Share Posted May 26, 2008 remove $text=nl2br($text); Link to comment https://forums.phpfreaks.com/topic/107250-creating-my-own-code-boxes-extra-linebreaks/#findComment-550403 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.