![]() |
Stuck with regexps in HTML again
Hi everyone:
I'm stuck with regular expressions again. Here's what I'm trying to accomplish. I need to add target=_blank to every hyperlink in the HTML. I use the following PHP code: PHP Code:
|
Wow, that sucks. I wrote a response and then restarted my computer to install SVN and poof, off into the ether it went because I forgot to post it.
Let's try again quick. Your regular expression isn't working the way you expect because it's being too greedy. It's looking for everything between the first opening element it finds and the last greater than sign in your $txt variable. You need to be careful using ".", and avoid it when there is a better solution, since your expression is matching: <a href="http://domain.com/">Go to domain.com!</a> php Code:
Please note that if you are going to use the above expression unmodified that it will not take into account anchors that already have a target attribute - it can and will add a second one. Also, it will add targets to named anchors. You will have to get a little more creative if you wish it to watch for those items. |
Yeah I know it sucks when it doesn't save. As a rule of thumb I always copy my response before I post it.
In the meantime you saved my butt again. Thanks! I like your second expression better, but I had to modify it to this: PHP Code:
|
Hah, I didn't want to leave you hanging when my first response was whisked off into oblivion, so I wrote that second one pretty fast. I put in backslashes when I should have used forward slashes, so that's just a straight up mistake on my part.
|
Yeah, thanks. Another thing that didn't make sense to me but evidently had to be removed was the > at the end, although you put ^> there. But again I don't know much about regexps. Speaking of which, do you use any technical reference for them, or how do you memorize all those symbols?
|
php Code:
Fixed. I left the closing sign out of my pattern because it matches up until then anyways, so why waste the two extra bytes I figure. I've memorized most of the symbols at this point. Ranges, quantifiers, characters and anchors are all pretty straight forward once you've used them a few times. What I need to learn next myself is assertations. While I can honestly say I've managed to create some pretty useful expressions without them, I really think learning them will take things up a level. They seem straightforward enough but I've just never put them into practice yet. php Code:
Which is nice if all of your anchors are formed in the order you expect them to be, such as a href, or a name, but if somebody decided to use the format a class="" name="" then the above will match it and add the target attribute. So once again, while the expression has gotten smarter, it's still not smart enough. There's a good cheat sheet over at addedbytes.com, and some great writeups at regular-expressions.info |
Thanks a bunch again. I'll be honest with you, I don't even know what assertations are :)
|
Well to begin learning what they are, the first thing you must do is ignore what I say. I incorrectly called them assertations, but what I meant to say was assertions.
This article has a good introduction to them. (Section 4.4) |
:) It seems like an in-depth article about regexps. I'll take a look, thanks.
|
| All times are GMT. The time now is 12:42 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0