ajicles Posted July 5, 2010 Share Posted July 5, 2010 I have a problem, I want to use PHP to search through a variable and take the text and make it into <a href> click-able link. But the only thing is there is a lot of other things inside of the same variable. I could use this but, it just only works with one thing in the variable: <?php function clickable_link($text) { $text = preg_replace('#(script|about|applet|activex|chrome):#is', "\\1:", $text); $ret = ' ' . $text; $ret = preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $ret); $ret = preg_replace("#(^|[\n ])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $ret); $ret = preg_replace("#(^|[\n ])([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $ret); $ret = substr($ret, 1); return $ret; } $text="www.google.com"; echo clickable_link($text); ?> Quote Link to comment Share on other sites More sharing options...
premiso Posted July 5, 2010 Share Posted July 5, 2010 You may find this link about laziness and greediness of help. http://www.phpfreaks.com/forums/index.php/topic,211115.msg963061.html#msg963061 You want to expressions to be greedy. Quote Link to comment Share on other sites More sharing options...
salathe Posted July 5, 2010 Share Posted July 5, 2010 I could use this but, it just only works with one thing in the variable: Huh? With $text="www.google.com is fun. ftp.goobar.com is not. http://link.me for short links!"; all three are wrapped in anchor tags as expected. Quote Link to comment Share on other sites More sharing options...
ajicles Posted July 5, 2010 Author Share Posted July 5, 2010 You may find this link about laziness and greediness of help. http://www.phpfreaks.com/forums/index.php/topic,211115.msg963061.html#msg963061 You want to expressions to be greedy. I could do this but I would have to make custom tags like <link> </link> and echo back the url. But with a chunk of text I would have to search for the beginning of http and follow that to the end of the url. Something like: preg_match('%http://(.+?) %s', $data, $matches); and the space after the url ends the link which would be proper? I'll try this and get back to you. Quote Link to comment Share on other sites More sharing options...
ajicles Posted July 5, 2010 Author Share Posted July 5, 2010 I tried using: <?php $data = " Everthing you need to know about LGU: http://www.legitgamingunion.com/ Web Designer: http://www.youtube.com/user/xIVIAJIKx Intro: http://www.youtube.com/user/dougangot... GFX: http://www.youtube.com/user/BazikG ROK: http://www.youtube.com/user/R0KL0bStEr"; preg_match('%http://(.+?) %s', $data, $matches); echo $matches[1]; ?> Still trying to figure this out. I use from http:// to a line break but I was wondering if I could use a for statement to get all the of the urls.. Quote Link to comment Share on other sites More sharing options...
ajicles Posted July 9, 2010 Author Share Posted July 9, 2010 *Bump*? Quote Link to comment 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.