A regular expression that matches the URL of the URL:
The code is as follows |
Copy Code |
(((F|HT) {1}tp://) [-a-za-z0-9@:%_\+.~#?&//=]+) |
Example: PHP regular expression automatically matches whether the string is a URL address, and automatically adds a link.
The code is as follows |
Copy Code |
<?php Function Autolink ($rurl) { $rurl = eregi_replace (((F|HT) {1}tp://) [-a-za-z0-9@:%_ \+.~#?&//=]+) ', ' <a href= ' \1 ', Target=_blank rel=nofollow>\1 </a> ', $rurl); //Rel=nofollow is telling the search engine not to crawl this link if Strpos ($rurl "http" = = FALSE) { $rurl = eregi_replace (' www.[ -a-za-z0-9@:%_\+.~#?&//=]+ ) ', ' <a href= ' http://\1 ' target=_blank rel=nofollow >\1</a> ', $rurl); }else{ $rurl = Eregi_replace ([[: Space:] () [{}]) (www.[ -a-za-z0-9@ :%_\+.~#?&//=]+) ', ' \1<a href= ' http://\2 ' target=_blank rel=nofol low>\2</a> ', $ Rurl); } return $rurl; } ? |
Calling the Autolink () function
The code is as follows |
Copy Code |
<?php $str = ' http://www.111cn.net '; echo Autolink ($STR); ?>
|
Example 1 Gets the example of the HTTPS opening
The code is as follows |
Copy Code |
https?:/ /([-\w\.] +) + (: \d+)? (/([\w/_\.] * (\?\s+)?)? PHP example:automatically link URL ' s inside text. $text = Preg_replace (' @ https?:/ /([-\w\.] +) + (: \d+)? (/([\w/_\.] * (\?\s+)?) @ ', ' <a href= ' >$1</a> ', $text); |
Example 2 directly extracts the URL address in <a in the page
The
code is as follows |
copy code |
function match_links ($document) { Preg_match_all ("' <\s*a\s.*?href\s*=\s* ([\] \])? (1) (.*?) \\1| ([^\s\>]+)] [^>]*>? (.*?) </a> ' Isx ', $document, $links); while ($key, $val) = each ($links [2])) { if (!empty ($val)) $match [' link '] [] = $val; } while (the list ($key, $val) = each ($links [3])) { if (!empty ($val)) $match [' link '] [] = $val; } while (the list ($key, $val) = each ($links [4])) { if (!empty ($val)) $match [' content '] = $val; } while (the list ($key, $val) = each ($links [0])) { if (!empty ($val)) $match [' All '] [] = $val; } Return $match; } |
Okay, here is the URL to get the address is also introduced, the above several examples of strengths, we can according to their own needs to choose more suitable for their own matching verification of the URL to extract URLs.