Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 4th, 2005, 6:15 AM   #1
Simon Gray
Programmer
 
Join Date: Aug 2005
Location: Ålsgårde, Denmark
Posts: 62
Rep Power: 4 Simon Gray is on a distinguished road
Send a message via MSN to Simon Gray
Regex help

How would I replace all <br /> tags inside a <pre></pre> block using the perl regular expressions function, preg_replace(), in PHP? I'm having a hard time accomplishing this.
__________________
Visit my blog, about all that stuff in between your nostrils >_>
Simon Gray is offline   Reply With Quote
Old Sep 4th, 2005, 6:37 AM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
You don't need preg_replace - just str_replace. Look it up: www.php.net
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Sep 4th, 2005, 7:12 AM   #3
Simon Gray
Programmer
 
Join Date: Aug 2005
Location: Ålsgårde, Denmark
Posts: 62
Rep Power: 4 Simon Gray is on a distinguished road
Send a message via MSN to Simon Gray
Uh no? Did you skip the part where it said inside a pre tag ? I don't want all <br />s replaced.
__________________
Visit my blog, about all that stuff in between your nostrils >_>
Simon Gray is offline   Reply With Quote
Old Sep 4th, 2005, 9:51 AM   #4
Lich
Professional Programmer
 
Lich's Avatar
 
Join Date: May 2005
Location: Detroit
Posts: 254
Rep Power: 4 Lich is on a distinguished road
Send a message via AIM to Lich Send a message via MSN to Lich
uhm, I dunno. Str_replace() maybe?
__________________
--John Cruz
Web Developer
www.cruzweb.net
Lich is offline   Reply With Quote
Old Sep 4th, 2005, 10:09 AM   #5
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
Quote:
Originally Posted by Lich
uhm, I dunno. Str_replace() maybe?
lol, that's what ooble just said, and that was wrong

I found this on the internet at http://www.cs.wcupa.edu/~rkline/perl2php/:
Perl:                   PHP:
$s =~ s/\s+/X/;         $s = preg_replace( "/\s+/", 'X', $s, 1 );
$s =~ s/\s+/X/g;        $s = preg_replace( "/\s+/", 'X', $s );
Polyphemus_ is offline   Reply With Quote
Old Sep 4th, 2005, 4:03 PM   #6
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
OK...
[php]$string = preg_replace('#(?<=<pre>.*)<br />(?=</pre>)#m', '', $string);[/php]
That should do it. I'll explain it when I've eaten my dinner.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Sep 6th, 2005, 3:02 PM   #7
Simon Gray
Programmer
 
Join Date: Aug 2005
Location: Ålsgårde, Denmark
Posts: 62
Rep Power: 4 Simon Gray is on a distinguished road
Send a message via MSN to Simon Gray
Gives me a parse error =/
__________________
Visit my blog, about all that stuff in between your nostrils >_>
Simon Gray is offline   Reply With Quote
Old Sep 6th, 2005, 3:53 PM   #8
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Argh... I'll work on it now. Gimme a few secs.

EDIT: got it. Just removed the fancy crap:
$stuff = preg_replace('#(<pre>.*)<br />(.*</pre>)#m', '$1$2', $stuff);
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Sep 7th, 2005, 8:22 AM   #9
Simon Gray
Programmer
 
Join Date: Aug 2005
Location: Ålsgårde, Denmark
Posts: 62
Rep Power: 4 Simon Gray is on a distinguished road
Send a message via MSN to Simon Gray
K, I was working on something similar, but the problem is it only replaces the last occurence of such a tag.

$text = "<pre>Sentence one<br />two<br />three</pre>";
$text = preg_replace('#(<pre>.*)<br />(.*</pre>)#m', '$1$2', $text);

This outputs

<pre>Sentence one<br />twothree</pre>
__________________
Visit my blog, about all that stuff in between your nostrils >_>
Simon Gray is offline   Reply With Quote
Old Sep 7th, 2005, 10:29 AM   #10
Simon Gray
Programmer
 
Join Date: Aug 2005
Location: Ålsgårde, Denmark
Posts: 62
Rep Power: 4 Simon Gray is on a distinguished road
Send a message via MSN to Simon Gray
Maybe it's because I'm misguided? I was under the impression preg_replace() worked recursively until all <br />s had been replaced, but maybe I'm wrong.

Anyway, this works for me, but I'm not happy about it =/

while (preg_match('#(<pre>.*)<br />(.*</pre>)#m', $text)) {
  		$text = preg_replace('#(<pre>.*)<br />(.*</pre>)#m', "$1\n$2", $text);
 }
__________________
Visit my blog, about all that stuff in between your nostrils >_>
Simon Gray is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:55 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC