Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Dec 31st, 2005, 5:10 PM   #1
para
Programmer
 
Join Date: Dec 2005
Posts: 65
Rep Power: 3 para is on a distinguished road
regex (preg_replace) parsing

I'm trying to replace certain occurances of a substring within certain substrings of a string.
For example, if I have the string:
"blah1<br>blah2 blah3 <pre>asdf<br>123<br></pre> blah4 blah5<br>blah6"
I would like to be able to use a preg_replace() call to change this to:
"blah1<br>blah2 blah3 <pre>asdf123</pre> blah4 blah5<br>blah6"
Where, in this example, <br>'s are removed, but only within <pre> blocks. Is this possible?

I'm not so great a PHP, and worse at regex; I came up with something like..
$str = preg_replace("/<pre>(.*)<br><\/pre>/", "<pre>$1</pre>", $str);
But it doesn't exactly work correctly. I'm sure this is far from what I want to

Might anyone be regex gurus?
para is offline   Reply With Quote
Old Dec 31st, 2005, 5:24 PM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Maybe you should take advantage of preg_replace_callback:
[PHP]function remove_br_tags($matches) {
return preg_replace("/<br>/", "", $matches[0]);
}

$str = "blah1<br>blah2 blah3 <pre>asdf<br>123<br></pre> blah4 blah5<br>blah6";

$str = preg_replace_callback("/<pre>.*?<\/pre>/", "remove_br_tags", $str);[/PHP]
Arevos is offline   Reply With Quote
Old Dec 31st, 2005, 6:30 PM   #3
para
Programmer
 
Join Date: Dec 2005
Posts: 65
Rep Power: 3 para is on a distinguished road
Thanks, worked great.
para 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 6:50 AM.

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