eMonk Posted January 29, 2012 Share Posted January 29, 2012 I'm try to add target="_blank" in the following a href so the link opens in a new tab. How can this be done? $description = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '\\1<a href="http://\\2">\\2</a>', $description); I tried <a href="http://\\2" target="_blank">\\2</a> but it didn't work. Quote Link to comment https://forums.phpfreaks.com/topic/255959-how-can-i-do-this/ Share on other sites More sharing options...
ragax Posted January 29, 2012 Share Posted January 29, 2012 Hi eMonk, Not sure where the first capture group fits in real life, but keeping it as is (only modifying Group 2), this works. Note that the input string starts with a } to give the regex something that matches the first capture group you specified. Input: }www.test.com Code: <?php $regex=',([[:space:]()[{}])(www\.[-a-z0-9@:%_\+.~#?&//=]+),i'; $string='}www.test.com'; echo htmlentities(preg_replace($regex,'\1<a href="http://\2" target="_blank">\2</a>',$string)); ?> Output: }<a href="http://www.test.com" target="_blank">www.test.com</a> If the first capture group is "old garbage", run this instead: Input: www.test.com Code: <?php $regex=',(www\.[-a-z0-9@:%_\+.~#?&//=]+),i'; $string='www.test.com'; echo htmlentities(preg_replace($regex,'<a href="http://\1" target="_blank">\1</a>',$string)); ?> Output: <a href="http://www.test.com" target="_blank">www.test.com</a> Is this what you're looking for? Quote Link to comment https://forums.phpfreaks.com/topic/255959-how-can-i-do-this/#findComment-1312094 Share on other sites More sharing options...
eMonk Posted January 29, 2012 Author Share Posted January 29, 2012 I have it in the a function. Here's the complete code: $description = $row['description']; function autolink($description) { $description = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '\\1<a href="http://\\2">\\2</a>', $description); return $description; } Quote Link to comment https://forums.phpfreaks.com/topic/255959-how-can-i-do-this/#findComment-1312113 Share on other sites More sharing options...
ragax Posted January 29, 2012 Share Posted January 29, 2012 Yes, so in your function, just pop in what I gave you. Without doing anything else to your code: function autolink($description) { $regex=',([[:space:]()[{}])(www\.[-a-z0-9@:%_\+.~#?&//=]+),i'; $description = preg_replace($regex,'\1<a href="http://\2" target="_blank">\2</a>',$description); return $description; } Or, for the second version, in case Group 1 in your regex is leftover garbage from something else: function autolink($description) { $regex=',(www\.[-a-z0-9@:%_\+.~#?&//=]+),i'; $description = preg_replace($regex,'<a href="http://\1" target="_blank">\1</a>',$description); return $description; } Quote Link to comment https://forums.phpfreaks.com/topic/255959-how-can-i-do-this/#findComment-1312125 Share on other sites More sharing options...
eMonk Posted January 29, 2012 Author Share Posted January 29, 2012 That worked but if the user enters in a url with http:// it doesn't get included in the a href link and appears as normal text. Also for email links: $description = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})', '<a href="mailto:\\1">\\1</a>', $description); See any an needed stuff here? Thanks for your help!! I'll be purchasing a book or read online tutorials how this stuff works in the upcoming weeks. It looks confusing at first glance. Quote Link to comment https://forums.phpfreaks.com/topic/255959-how-can-i-do-this/#findComment-1312138 Share on other sites More sharing options...
ragax Posted January 29, 2012 Share Posted January 29, 2012 That worked but if the user enters in a url with http:// it doesn't get included Okay, you're changing the spec on me. Slight change to the regex then, with an optional http:// $regex=',(?:http://)?(www\.[-a-z0-9@:%_\+.~#?&//=]+),i'; See any unneeded stuff here? I have to tell you that the movement is away from the ereg functions, now deprecated, and toward the preg functions. In a future version of PHP, they will drop off. I would guess that this would fix your code (didn't have time to test it though, past midnight here): $description = preg_replace('~([_.0-9a-z-]++@([0-9a-z-]{2,}+\.)+[a-z]{2,3})~i', '<a href="mailto:\1">\1</a>', $description); Let me know if that works for you. Quote Link to comment https://forums.phpfreaks.com/topic/255959-how-can-i-do-this/#findComment-1312157 Share on other sites More sharing options...
eMonk Posted January 30, 2012 Author Share Posted January 30, 2012 That worked but now it's removing http:// from the text. How can you keep it intact? Thanks for your help playful. Much appreciated!! Quote Link to comment https://forums.phpfreaks.com/topic/255959-how-can-i-do-this/#findComment-1312600 Share on other sites More sharing options...
ragax Posted January 30, 2012 Share Posted January 30, 2012 Without testing: function autolink($description) { $regex=',(http://)?(www\.[-a-z0-9@:%_\+.~#?&//=]+),i'; $description = preg_replace($regex,'<a href="http://\2" target="_blank">\1\2</a>',$description); return $description; } The http only gets inserted if the text if it's there originally. If you want it there all the time: function autolink($description) { $regex=',(?:http://)?(www\.[-a-z0-9@:%_\+.~#?&//=]+),i'; $description = preg_replace($regex,'<a href="http://\1" target="_blank">http://\1</a>',$description); return $description; } Quote Link to comment https://forums.phpfreaks.com/topic/255959-how-can-i-do-this/#findComment-1312743 Share on other sites More sharing options...
eMonk Posted January 31, 2012 Author Share Posted January 31, 2012 The http text is there originally. Don't want it there all the time just when the user enters it in $description. Quote Link to comment https://forums.phpfreaks.com/topic/255959-how-can-i-do-this/#findComment-1312798 Share on other sites More sharing options...
ragax Posted January 31, 2012 Share Posted January 31, 2012 The http text is there originally. Don't want it there all the time just when the user enters it in $description. Okay, so did my last post solve it, or do you need more help? Quote Link to comment https://forums.phpfreaks.com/topic/255959-how-can-i-do-this/#findComment-1312832 Share on other sites More sharing options...
eMonk Posted January 31, 2012 Author Share Posted January 31, 2012 It's working now. Weird, the code I had wasn't the same as the one you posted. I probably need more sleep. Thanks again playful!! Quote Link to comment https://forums.phpfreaks.com/topic/255959-how-can-i-do-this/#findComment-1312862 Share on other sites More sharing options...
ragax Posted January 31, 2012 Share Posted January 31, 2012 You're welcome bro, it's a pleasure. Quote Link to comment https://forums.phpfreaks.com/topic/255959-how-can-i-do-this/#findComment-1312876 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.