PHP Regular expression matching verification extraction URL instance summary

Source: Internet
Author: User
Tags php example php regular expression regular expression

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.