Regular Expressions-match URLs in PHP strings

Source: Internet
Author: User
Tags url example
Demand:

客户端传过来一段字符串,需要从字符串中匹配出所有的url,包括域名或IP后面的参数(含端口)

URL Example:

http://127.0.0.1/metinfo/img/img.php?class1=1&serch_sql=%201=if%28ascii%28substr%28user%28%29,1,1%29%29=114,1,2%29%23或者http://www.baidu.com/metinfo/img/img.php?class1=1&serch_sql=%201=if%28ascii%28substr%28user%28%29,1,1%29%29=114,1,2%29%23

Of course, simple URLs are also to be matched.
Solving the regular

Reply content:

Demand:

客户端传过来一段字符串,需要从字符串中匹配出所有的url,包括域名或IP后面的参数(含端口)

URL Example:

http://127.0.0.1/metinfo/img/img.php?class1=1&serch_sql=%201=if%28ascii%28substr%28user%28%29,1,1%29%29=114,1,2%29%23或者http://www.baidu.com/metinfo/img/img.php?class1=1&serch_sql=%201=if%28ascii%28substr%28user%28%29,1,1%29%29=114,1,2%29%23

Of course, simple URLs are also to be matched.
Solving the regular

Match all URLs with a fairly broad regular, such as

https?:\/\/\S+

And then take the function for this heap of URLs in turn parse_url

^(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*$http://regexlib.com/Search.aspx?k=url&c=-1&m=5&ps=20

Java probably wrote this.

String str = "接收到的字符串"String regex = "(http:|https:)//[^[A-Za-z0-9,:\\._\\?%&+\\-=/#]]*";        Pattern pattern = Pattern.compile(regex);        Matcher matcher = pattern.matcher(str);        while (matcher.find()) {            String url=matcher.group();            System.out.println(url);        }

The following string passes the test.

String str="http://127.0.0.1:6666/ " +                "https://www.baidu.com/ " +                "http://127.0.0.1/metinfo/img/img.php?class1=1&serch_sql=%201=if%28ascii%28substr%28user%28%29,1,1%29%29=114,1,2%29%23\n" +                "或者\n" +                "哈哈http://www.baidu.com:85676/metinfo/img/img.php?class1=1&serch_sql=%201=if%28ascii%28substr%28user%28%29,1,1%29%29=114,1,2%29%23 6666都是对的";

输出

http://127.0.0.1:6666/https://www.baidu.com/http://127.0.0.1/metinfo/img/img.php?class1=1&serch_sql=%201=if%28ascii%28substr%28user%28%29,1,1%29%29=114,1,2%29%23http://www.baidu.com:85676/metinfo/img/img.php?class1=1&serch_sql=%201=if%28ascii%28substr%28user%28%29,1,1%29%29=114,1,2%29%23

What, you asked for PHP? Sorry, I do not PHP ...
Just the same, you have to move your head.

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