View Single Post
Old 03-22-2010, 05:43 PM   #2 (permalink)
delayedinsanity
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)