aximbigfan Posted November 23, 2008 Share Posted November 23, 2008 Hi, I have a PHP RSS reader, and one problem is that the links in some RSS "posts" will open in the same window. What I would like to do, is make a function to insert "target='_blank'" into each link. How would I do this? Thanks, Chris Link to comment https://forums.phpfreaks.com/topic/133844-change-all-hyperlinks-to-open-in-new-window/ Share on other sites More sharing options...
JasonLewis Posted November 23, 2008 Share Posted November 23, 2008 I use JavaScript for this, only because the target attribute is not supported in XHTML. So the way around it is to use JavaScript to set the target attribute. I also use jQuery, to speed up the process. To define links that are to open in a new window I use the rel attribute and make it equal external. <a href="http://www.google.com" rel="external">Google</a> Then the jQuery: $(document).ready(function(){ $('a[rel="external"]').attr("target","_blank"); }); Link to comment https://forums.phpfreaks.com/topic/133844-change-all-hyperlinks-to-open-in-new-window/#findComment-696624 Share on other sites More sharing options...
aximbigfan Posted November 23, 2008 Author Share Posted November 23, 2008 Problem is that I would still have to change all the links so that they reference rel, right? I have a huge string with the html in it, and I need to search each <a out and insert a "target='_blank' into it. I don't care about XHTML. The page will always be viewed on FF. chris Link to comment https://forums.phpfreaks.com/topic/133844-change-all-hyperlinks-to-open-in-new-window/#findComment-696687 Share on other sites More sharing options...
aximbigfan Posted November 23, 2008 Author Share Posted November 23, 2008 Never mind. I got it. $description = preg_replace('/<a/', '<a target=\'_blank\'', $description); Not pretty, and won't work for image links, but it works. Chris Link to comment https://forums.phpfreaks.com/topic/133844-change-all-hyperlinks-to-open-in-new-window/#findComment-696690 Share on other sites More sharing options...
br3nn4n Posted November 23, 2008 Share Posted November 23, 2008 You got it, use regex. That's how I would do it. Link to comment https://forums.phpfreaks.com/topic/133844-change-all-hyperlinks-to-open-in-new-window/#findComment-696701 Share on other sites More sharing options...
aximbigfan Posted November 23, 2008 Author Share Posted November 23, 2008 You got it, use regex. That's how I would do it. Eventually, I'll mod it out to support any tag with a href directive in it. I just don't really get along with RegExp that well. Myabe I should take some time off and learn it.. Chris Link to comment https://forums.phpfreaks.com/topic/133844-change-all-hyperlinks-to-open-in-new-window/#findComment-696778 Share on other sites More sharing options...
br3nn4n Posted November 24, 2008 Share Posted November 24, 2008 I just don't really get along with RegExp that well. Haha like any of the rest of us do? Link to comment https://forums.phpfreaks.com/topic/133844-change-all-hyperlinks-to-open-in-new-window/#findComment-697335 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.