PHP simple way to match URLs with regular Expressions _php tutorial

Source: Internet
Author: User
An alternative to the Parse_url () function seen on the official web site of PHP. The result is similar to the Parse_url () function, and is implemented using a regular. URIs are every resource available on the Web-HTML documents, images, video clips, programs, and so on-positioned by a common resource identifier (Uniform Resource Identifier, referred to as the "URI"). Group objects:
Copy CodeThe code is as follows:
^(([^:/?#]+):)? (//([^/?#]*))? ([^?#]*) (\? ([^#]*))? (#(.*))?
12 3 4

The test code is as follows:
Copy CodeThe code is as follows:
$search = ' ~^ (([^:/?#]+):)? ( //([^/?#]*))? ([^?#]*) (\? ([^#]*))? (# (. *))? ~i ';
$url = ' http://www.jb51.net/pub/ietf/uri/#Gonn ';
$url = Trim ($url);
Preg_match_all ($search, $url, $RR);
printf ("

The output URL data is:

%s
\ n ", Var_export ($RR, TRUE));

/*
The groupings are as follows
$ = http:
$ = http
$ $ =//www.nowamagic.net
$4 = Www.nowamagic.net
$ $ =/pub/ietf/uri/
$6 =
$7 =
$8 = #Gonn
$9 = Gonn
*/
?>

The preceding regular expression can get any part of the URL, and the following code is simple:
Copy the Code code as follows:
Get host name from URL
Preg_match ("/^ (http:\/\/)?" ( [^\/]+)/I "," http://www.jb51.net/index.html ", $matches);
$host = $matches [2];
Get the next two paragraphs from the hostname
Preg_match ("/[^\.\/]+\.[ ^\.\/]+$/", $host, $matches);
echo "Domain name is: {$matches [0]}\n";
?>

http://www.bkjia.com/PHPjc/825116.html www.bkjia.com true http://www.bkjia.com/PHPjc/825116.html techarticle An alternative to the Parse_url () function seen on the official web site of PHP. The result is similar to the Parse_url () function, and is implemented using a regular. URIs are every resource available on the Web-HTML documents, images ...

  • 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.