Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
06-14-2006, 05:14 AM #1
proper search pattern for the following
I've been trying to get this for the last 2 hours now and I can't seem to get the right pattern for this.
I'm trying to make it so if certain tags are replaced with new ones, and if <br> is found before, or after any of the original tags, it takes them out.
for example, i would want the following text...
$text = 'this is my text<br>[text]<br>here is some text<br>[/text]<br>';
$text = 'this is my text<div class=display>here is some text</div>';
Here's what I have so far..
$text = preg_replace("/(<br>)\[code\](<br>)(.*?)(<br>)\[\/code\](<br>)/ism","\n<div style='width:50%;background-color:#cccccc;'>$3</div>\n",$text);
I've been reading about the regular expressions but I just can't figure it out
06-14-2006, 06:04 AM #2
Here's what I'd do with the regex:
The changes are:PHP Code:
$text = 'this is my text<br>[code]<br>here is some text[/code]<br>';
'/(?:<br[\s\/]*>)* # Match <br followed by an number of spaces and forward slashes (including zero) and terminated by a gt, the whole group any number of times including zero and not captured
\[code\] # Match the tag
(?:<br[\s\/]*>)* # More optional break tags
(.*?) # Content, capture this group
(?:<br[\s\/]*>)* # More optional breaks
\[\/code\] # The ending tag
(?:<br[\s\/]*>)* # Trim as many break tags as possible from the end
$text = preg_replace($regex, '<div class="display">$1</div>', $text);
(a) (the one that specifically addresses your question) adding * after a group means match any number of times, *including zero*. This makes the breaks optional. If you only want them to match once, postfix the group with a question mark to make it optional. Eg., /net(?:work)?\sstatus/ matches 'net status' and 'network status'.
(b) using ?: at the beginning of a group indicates that it's a semantic group and not a capturing group. It saves a bit of memory and (I think) facilitates understanding.
(c) adding [\s\/]* matches as many spaces and forward slashes as possible within breaks, so you'll catch XHTML style <br/> and <br /> as well as <br> (along with a bunch of malformed junk like <br/ / // / / /////>, but there's no harm in filtering that out)
06-14-2006, 07:33 AM #3
wow! thanks that helps alot and I have a better understanding of these patterns
06-14-2006, 09:18 AM #4
btw, how would I now make it so any [b] or [/b] ..etc won't parse unless it's outside the [code] [/code] tags?
eg. I have this code:
But it doesn't work.. it just continues parsing itCode:$text = preg_replace("/(?!\[code\](.*?))\[color=(.*?)\](.*?)\[\/color\](?!(.*?)\[\/code\])/","<span style='color:$2;'>$3</span>",$text);
What's the proper pattern for this?
to better understand what i'm trying to do, is i want the equivalent of CF's [noparse] and [/noparse]