Regular expressions matching URLs in php

Source: Internet
Author: User

URI is the type of resources available on the Web-HTML documents, images, video clips, programs, etc.-are located by an Uniform Resource Identifier ("URI.

Grouping:

^ ([^ :/? #] + ):)? (// ([^ /? #] *)? ([^? #] *) (? ([^ #] *)? (#(.*))?
12 3 4 5 6 7 8 9

Example

 

The code is as follows: Copy code

Regular expression (http | ftp | https): // [w-_] + (. [w-_] +) + ([w -.,@? ^ = % & Amp ;:/~ + #] * [W -@? ^ = % & Amp ;/~ + #])?
Matching http://regxlib.com/Default.aspx | http://electronics.cnet.com/electronics/0-6342366-8-8994967-1.html
Does not match www.yahoo.com
 
Regular expression ^ \ {2} [w-] + \ ([w-] [w-s] * [w-] + [$]? $) | ([W-] [$]? $ ))
Matching \ serverservice | \ servermy service | \ serv_001service $
Mismatch \ my serverservice | \ server service | \ server $ service

Regular expression ^ (http | https | ftp): // ([a-zA-Z0-9.-] + (: [a-zA-Z0-9. & amp; % $-] + )*@)? (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1 -9] {1} [0-9] {1} | [1-9]). (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1- 9] {1} [0-9] {1} | [1-9] | 0 ). (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1- 9] {1} [0-9] {1} | [1-9] | 0 ). (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1- 9] {1} [0-9] {1} | [0-9]) | ([a-zA-Z0-9-] + .) * [a-zA-Z0-9-] +. [a-zA-Z] {2, 4}) (: [0-9] + )? (/[^/] [A-zA-Z0-9 .,? '\/+ & Amp; % $ # = ~ _-@] *) * $
Match http://www.sysrage.net | https: // 64.81.85.161/site/file. php? Cow = moo's | ftp: // user: pass@host.com: 123
Does not match sysrage.net

Regular expression ^ ([a-zA-Z]: |\[ ^ /\:*? "<> |] + \ [^ /\:*? "<> |] +) (\ [^ /\:*? "<> |] +) + (. [^ /\:*? "<> |] +) $
Matching c: Test.txt | \ serversharedTest.txt | \ serversharedTest. t
Does not match c: Test | \ servershared | \ serversharedTest .?


Regular expression ^ (http | https | ftp): // ([a-zA-Z0-9. -] + (: a-zA-Z0-9. & amp; % $-] + )*@) * (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1-9] {1} [0-9] {1} | [1-9]). (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1- 9] {1} [0-9] {1} | [1-9] | 0 ). (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1- 9] {1} [0-9] {1} | [1-9] | 0 ). (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1- 9] {1} [0-9] {1} | [0-9]) | localhost | ([a-zA-Z0-9-] + .) * [a-zA-Z0-9-] +. (com | edu | gov | int | mil | net | org | biz | arpa | Info | name | pro | aero | coop | museum | [a-zA-Z] {2}) (: [0-9] +) * (/($ | [a-zA-Z0-9 .,? '\ + & Amp; % $ # = ~ _-] +) * $
Match http://site.com/dir/file.php? Var = moo | https: // localhost | ftp: // user: pass@site.com: 21/file/dir
Does not match site.com | http://site.com/dir //


Regular expression ^ ([a-zA-Z] :) (\ [^ \/:*? <> "|] * (? <! []) * (. [A-zA-Z] {2, 6}) $
Match C: di___rfi_sysle.txt | c: dirfilename.txt
Does not match c: dirfile? Name.txt


Regular expression ^ ([a-zA-Z0-9] ([a-zA-Z0-9-] {} [a-zA-Z0-9])?.) + [A-zA-Z] {2, 6} $
Match regexlib.com | this. is. a. museum | 3com.com
Unmatched notadomain-.com | helloworld. c | .oops.org


Regular expression ^ (ht | f) tp (s ?)) ://)? (Www. | [a-zA-Z].) [a-zA-Z0-9.] +. (com | edu | gov | mil | net | org | biz | info | name | museum | us | ca | uk) (: [0-9] +) * (/($ | [a-zA-Z0-9 .,;? '\ + & Amp; % $ # = ~ _-] +) * $
Match www.blh.com: 8103 | www.blh.com/blh.asp? Sort = ASC | www.blh.com/blh.htm?blah
Does not match www. state. ga | http://www.blah.ru


Regular expression B ([w-] + ://? | Www [.]) [^ s () <>] + (? :( [Wd] +) | ([^ [: punct:] s] | /)))
Matching http://foo.com/blah_blah | http://foo.com/blah_blah/ | (Something like http://foo.com/blah_blah) | http://foo.com/blah_blah_ (wikipedia) | (Something like http://foo.com/blah_blah_ (wikipedia) | http://foo.com/blah_blah. http://foo.com/blah_blah. | Does not match no_ws.example.com | no_proto_or_ws.com |/relative_resource.php


Can match any URL

The code is as follows: Copy code

<Script type = "text/javascript">
Function IsURL (urlString)
{
RegExp =/^ (https? | Ftp | news )://)? ([A-z] ([a-z0-9-] * [..]) + ([A-z] {2} | aero | arpa | biz | com | coop | edu | gov | info | int | jobs | mil | museum | name | nato | net | org | pro | travel) | ([0-9] | [1-9] [0-9] | 1 [0-9] {2} | 2 [0-4] [0-9] | 25 [0-5]).) {3} ([0-9] | [1-9] [0-9] | 1 [0-9] {2} | 2 [0-4] [0-9- 9] | 25 [0-5]) (/[a-z0-9 _-. ~] +) * (/([A-z0-9 _-.] *) (? [A-z0-9 + _-. % = &] *)? (# [A-z] [a-z0-9 _] *)? $/
If (urlString. match (regExp ))
Return "is a url ";
Else
Return "not a url ";
}
Url = "";
Alert (IsURL (url ));
</Script>


PHP testing:

The code is as follows: Copy code


<? Php
$ Search = '~ ^ ([^ :/? #] + ):)? (// ([^ /? #] *)? ([^? #] *) (? ([^ #] *)? (#(.*))?~ I ';
$ Url = 'http: // www.php.net/pub/ietf/uri/#related ';
$ Url = trim ($ url );
Preg_match_all ($ search, $ url, $ rr );
Printf ("<p> output URL data: </p> <pre> % s </pre> n", var_export ($ rr, TRUE ));

/*
The groups are as follows:
$1 = http:
$2 = http
$3 = // www.php.net
$4 = www.php.net
$5 =/pub/ietf/uri/
$6 = <undefined>
$7 = <undefined>
$8 = # Related
$9 = Related
*/
?>

Baidu saw another simple code:

 

The code is as follows: Copy code
<? Php
// Obtain the host name from the URL
Preg_match ("/^ (http ://)? ([^/] +)/I "," http://www.php.net/index.html ", $ matches );
$ Host = $ matches [2];
// Obtain the following two segments from the host name
Preg_match ("/[^./] +. [^./] + $/", $ host, $ matches );
Echo "domain name is: {$ matches [0]} n ";
?>

Output after execution: domain name is: php.net

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.