![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
|
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 >_> |
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
You don't need preg_replace - just str_replace. Look it up: www.php.net
|
|
|
|
|
|
#3 |
|
Programmer
|
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 >_> |
|
|
|
|
|
#4 |
|
Professional Programmer
|
uhm, I dunno. Str_replace() maybe?
|
|
|
|
|
|
#5 | |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
Quote:
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 ); |
|
|
|
|
|
|
#6 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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. |
|
|
|
|
|
#7 |
|
Programmer
|
Gives me a parse error =/
__________________
Visit my blog, about all that stuff in between your nostrils >_> |
|
|
|
|
|
#8 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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); |
|
|
|
|
|
#9 |
|
Programmer
|
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 >_> |
|
|
|
|
|
#10 |
|
Programmer
|
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 >_> |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|