![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Dec 2005
Posts: 65
Rep Power: 3
![]() |
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" "blah1<br>blah2 blah3 <pre>asdf123</pre> blah4 blah5<br>blah6" 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);![]() Might anyone be regex gurus? |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
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] |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Dec 2005
Posts: 65
Rep Power: 3
![]() |
Thanks, worked great.
![]() |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|