Page 1 of 2
what is this doing?
Posted: Sun Sep 25, 2005 8:57 am
by s.dot
So I'm wanting to replace anything between <style></style> tags.. with nothing. So very stupidly (;D) i came up with this regex.
Code: Select all
preg_replace('|<style(.+)/style>|i', '', $description);
Can anyone think of a case where this wouldn't match style tags?
Posted: Sun Sep 25, 2005 9:02 am
by feyd
- if they are on seperate lines
- if they have whitespace between the < or > or / and the tag name
- if you have multiple in the page all data between the outside tags will be removed as well
Posted: Sun Sep 25, 2005 9:07 am
by s.dot
Code: Select all
preg_replace('|<(\s*)style(.+)/(\s*)style(\s*)>|i', '', $string);
So that pretty much takes care of the whitespace?
Now on different lines.. should it matter? the .(period) matches any character.. does this include new lines?
Don't know what you meant by #3.
Posted: Sun Sep 25, 2005 9:11 am
by feyd
scrotaye wrote:Code: Select all
preg_replace('|<(\s*)style(.+)/(\s*)style(\s*)>|i', '', $string);
So that pretty much takes care of the whitespace?
basically, yes.
scrotaye wrote:Now on different lines.. should it matter? the .(period) matches any character.. does this include new lines?
The dot will not match new lines, because that is a separate setting.
scrotaye wrote:Don't know what you meant by #3.
After you fix the single line issue you'll encounter this:
Code: Select all
<style>
.someClass { margin-top: 3px; }
</style>
<button>hi</button>
<style>
.someOtherClass { margin-top: -3px; }
</style>
All of that will be wiped out.
Posted: Sun Sep 25, 2005 9:20 am
by s.dot
Code: Select all
preg_replace('|<style(.+)/style>|ism', '', $string);
So, reading d11wtq's tutorial adding the s after the delimiter ignores whitespace.. good.. that takes care of the spaces also..? And m is multi-line mode.. so that takes care of the ones on separate lines. Right?
I need to develop an environment to test this in.
Posted: Sun Sep 25, 2005 9:23 am
by feyd
scrotaye wrote:I need to develop an environment to test this in.
That would be a good idea...
Posted: Sun Sep 25, 2005 9:30 am
by s.dot
I just did that quickly. Wrote a page that echos out given $string then echos $string ran through the regex =)
I have #1 and #2 taken care of. Now for the multiple matches thing..
I tried:
Code: Select all
preg_replace("|(^<style(.+)/style>$)|ism","",$string);
heh.. didn't exactly work out too well. could you send me in the right direction

Posted: Sun Sep 25, 2005 9:57 am
by feyd
hint: ungreedy
Posted: Sun Sep 25, 2005 10:18 am
by s.dot
I know I need to change the (.+) to match anything except </style>
I think...
Posted: Sun Sep 25, 2005 10:40 am
by s.dot
I get the hint ungreedy as in the .+ is being greedy and matching too much stuff

but perhaps another hint?
Posted: Sun Sep 25, 2005 10:46 am
by feyd
there are 5 threads in the regex board that use the term ungreedy in them. 1 is this thread... the other 4 have explanations about it in them..

Posted: Sun Sep 25, 2005 10:53 am
by s.dot
lmao dammit feyd, you could've just told me to add a ? after the .+
But then again, what fun is that? That particular problem is solved
Unless you can think of another instance where
Code: Select all
preg_replace('|<style(.+?)/style>|ism', '', $string);
wouldn't match a given string.
Posted: Sun Sep 25, 2005 10:58 am
by feyd
send a bunch of variants at it.. if they all pass, you may be done.

Posted: Sun Sep 25, 2005 11:13 am
by pilau
And make a note, scrotaye: this would be silly to test:

Posted: Sun Sep 25, 2005 11:44 am
by s.dot
that wouldn't be bad to test

I would expect it to just leave the last </style> tag.. in which case strip_tags would get rid of that
