TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 03-22-2010, 07:29 AM   #1 (permalink)
The Wanderer
 
Join Date: Mar 2010
Posts: 9
Thanks: 2
dc2000 is on a distinguished road
Default 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:
$txt preg_replace("/<a\s+(.+)>/i""<a \\1 target=_blank>"$txt); 
But that seems to add it only to </a> tags. Why?
dc2000 is offline  
Reply With Quote
Old 03-22-2010, 05:43 PM   #2 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

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:

&lt;a href="http://domain.com/">Go to domain.com!</a>

php Code:
// Your expression, fixed:
$txt = preg_replace( '~<a\s+(.+?)>~i', '<a //1 target="_blank">', $txt );

// I would do it like this:
$txt = preg_replace( '~<a([^>]+)~i', '<a//1 target="_blank">', $txt );

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.
delayedinsanity is offline  
Reply With Quote
The Following User Says Thank You to delayedinsanity For This Useful Post:
dc2000 (03-22-2010)
Old 03-22-2010, 08:59 PM   #3 (permalink)
The Wanderer
 
Join Date: Mar 2010
Posts: 9
Thanks: 2
dc2000 is on a distinguished road
Default

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:
$txt preg_replace('~<a([^>]+)~i''<a\\1 target="_blank"',  $txt); 
IDK maybe I use a different PHP version?
dc2000 is offline  
Reply With Quote
Old 03-22-2010, 10:06 PM   #4 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

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.
delayedinsanity is offline  
Reply With Quote
Old 03-23-2010, 07:26 AM   #5 (permalink)
The Wanderer
 
Join Date: Mar 2010
Posts: 9
Thanks: 2
dc2000 is on a distinguished road
Default

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?
dc2000 is offline  
Reply With Quote
Old 03-23-2010, 04:17 PM   #6 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

php Code:
$txt = preg_replace( '~<a([^>]+)~i', '<a\\1 target="_blank"', $txt );

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:
// For example, this matches <a href=""> but excludes <a name="">
$txt = preg_replace( '~<a\s+(?!name)([^>]+)>~i', '<a \\1 target="_blank">', $txt );

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
delayedinsanity is offline  
Reply With Quote
Old 03-25-2010, 03:11 AM   #7 (permalink)
The Wanderer
 
Join Date: Mar 2010
Posts: 9
Thanks: 2
dc2000 is on a distinguished road
Default

Thanks a bunch again. I'll be honest with you, I don't even know what assertations are :)
dc2000 is offline  
Reply With Quote
Old 03-25-2010, 04:28 AM   #8 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

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)
delayedinsanity is offline  
Reply With Quote
Old 03-25-2010, 05:19 PM   #9 (permalink)
The Wanderer
 
Join Date: Mar 2010
Posts: 9
Thanks: 2
dc2000 is on a distinguished road
Default

:) It seems like an in-depth article about regexps. I'll take a look, thanks.
dc2000 is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Html 5 Randy XHTML, HTML, CSS 12 04-04-2013 09:26 AM
Exciting Stuff in HTML 5! Wildhoney XHTML, HTML, CSS 20 02-17-2013 03:16 PM
Trouble using regexps to explude HTML tags dc2000 General 6 03-18-2010 08:39 PM
Changing &eacute; into html entities without touching the html elements ? Peuplarchie General 1 11-10-2009 12:12 PM
Replace special chars not tags, php trigered by html Peuplarchie General 0 09-20-2009 02:32 PM


All times are GMT. The time now is 12:17 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design