meman1188 Posted May 14, 2008 Share Posted May 14, 2008 I'm looking for a perl expression to extract urls from text. The more flexible it is the better. For example, it would be nice if it could find all of these: http://www.yahoo.com yahoo.com www.yahoo.com/mypage/ Thanks so much for the help! Quote Link to comment https://forums.phpfreaks.com/topic/105555-perl-expression-to-extract-url-from-text/ Share on other sites More sharing options...
effigy Posted May 14, 2008 Share Posted May 14, 2008 <pre> <?php $data = <<<DATA http://www.yahoo.com yahoo.com www.yahoo.com/mypage/ DATA; echo preg_replace_callback('% ### Protocol or start. (?: https?:// | www\. | ### Add to as needed. \w+\.(?:com|net|org) ) ### Body. (?> ### Gobble all non-space \S+ ### Avoid ending punctuation. (?<!\p{P}) )? %x', create_function( '$url', '$url = $url[0]; if (substr($url, 0, 7) != "http://") { $url = "http://" . $url; } return "<a href=\"$url\">$url</a>";' ), $data ); ?> </pre> Quote Link to comment https://forums.phpfreaks.com/topic/105555-perl-expression-to-extract-url-from-text/#findComment-540939 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.