jdeutsch Posted January 1, 2008 Share Posted January 1, 2008 I recently found the following code on php.net listed under preg_replace. It does exactly what I'm trying to accomplish, however, it does the capture the TLD of the domain in question. CODE (extra spaces in regex added to keep code from running too long on the page): <?php function hyperlink($text) { // match protocol://address/path/file.extension?some=variable&another=asf% $text = preg_replace("/\s([a-zA-Z]+:\/\/[a-z][a-z0-9\_\.\-]* [a-z]{2,6}[a-zA-Z0-9\/\*\-\?\&\%\=]*)([\s|\.|\,])/i", " <a href=\"$1\" target=\"_blank\">$1</a>$2", $text); // match www.something.domain/path/file.extension?some=variable&another=asf% $text = preg_replace("/\s(www\.[a-z][a-z0-9\_\.\-]* [a-z]{2,6}[a-zA-Z0-9\/\*\-\?\&\%\=]*)([\s|\.|\,])/i", " <a href=\"http://$1\" target=\"_blank\">$1</a>$2", $text); // match name@address $text = preg_replace("/\s([a-zA-Z][a-zA-Z0-9\_\.\-]*[a-zA-Z]* \@[a-zA-Z][a-zA-Z0-9\_\.\-]*[a-zA-Z]{2,6})([\s|\.|\,])/i", " <a href=\"mailto://$1\">$1</a>$2", $text); return $text; } ?> For example, when attempting to extract http://www.google.com from a string, it gets "http://www.google" and nothing else. I am not very familiar with regular expressions, so if someone could tell me how to tweak the expressions to include the TLD (and any info after the domain - get variables, etc.), it would be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/83924-preg_replace-for-urls/ Share on other sites More sharing options...
teng84 Posted January 1, 2008 Share Posted January 1, 2008 i try this hyperlink('http://www.google.com ') nad it output http://www.google.com is there a problem there sorry cant understand it clearly.. and this should be on the regex board? Quote Link to comment https://forums.phpfreaks.com/topic/83924-preg_replace-for-urls/#findComment-427109 Share on other sites More sharing options...
jdeutsch Posted January 1, 2008 Author Share Posted January 1, 2008 Thanks for the pointer, and sorry about posting in the wrong place. Works fine, like you said. Quote Link to comment https://forums.phpfreaks.com/topic/83924-preg_replace-for-urls/#findComment-427144 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.