PHP matches URL Regular expression code

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags .url code content domain expression links net php

PHP Tutorials Match URL Regular expression code
This is a PHP regular expression that gets all the links in the article content
$str = "";

$reg = "<a[ss]*?" (href) s*=s* (? =["']) ((["]) (? <href>[^ "']*) 2) | (? <src>[^s>]+)) [ss]*?> "//The following example is to get the domain name regular expression in the content

Function Get_domain ($url) {
  $pattern =/[w-]+. ( COM|NET|ORG|GOV|CC|BIZ|INFO|CN) (. ( CN|HK)).
  Preg_match ($pattern, $url, $matches);
  if (count ($matches) > 0) {
   return $matches [0];
 }else{
   $rs = Parse _url ($url);
   $main _url = $rs ["Host"];
   if (!strcmp (Long2ip (sprintf ("%u", Ip2long ($main _url)), $main _url)) {
    return $ Main_url;
  }else{
    $arr = Explode (".", $main _url);
    $count =count ($ ARR);
    $endArr = Array ("com", "net", "org", "3322");//com.cn  net.cn
    if ( In_array ($arr [$count-2], $ENDARR) {
     $domain = $arr [$count-3]. "." $arr [$count-2]. $arr [$count-1];
   }else{
     $domain =  $arr [$count-2]. "." $arr [$count-1];
   }
    return $domain;
  }
 }
 
 
 //instance
 
  $STR = "Jfkdlajfdafdjak; Www.jzread.com ";
 echo Get_domain ($str)
 
 //The value is jzread.com
 

Related Article

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.