Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用正则表达式进行正确的url解析_Php_Regex - Fatal编程技术网

Php 使用正则表达式进行正确的url解析

Php 使用正则表达式进行正确的url解析,php,regex,Php,Regex,我有一个来自Wordpress的正则表达式。但是我无法正确地调整它来解析URL。我只想解析http/https/ftp链接 我试图更改方案部分:(http | https | ftp)+://,但它不起作用 对于URL:testhttp://google.com 匹配项应为:http://google.com 当前匹配:testhttp://google.com 谢谢 ~ ([\\s(<.,;:!?])

我有一个来自Wordpress的正则表达式。但是我无法正确地调整它来解析URL。我只想解析
http/https/ftp
链接

我试图更改方案部分:
(http | https | ftp)+://
,但它不起作用

对于URL:
testhttp://google.com

匹配项应为:
http://google.com

当前匹配:
testhttp://google.com

谢谢

~
    ([\\s(<.,;:!?])                                        # 1: Leading whitespace, or punctuation
    (                                                      # 2: URL
        [\\w]{1,20}+://                                # Scheme and hier-part prefix
        (?=\S{1,2000}\s)                               # Limit to URLs less than about 2000 characters long
        [\\w\\x80-\\xff#%\\~/@\\[\\]*(+=&$-]*+         # Non-punctuation URL character
        (?:                                            # Unroll the Loop: Only allow puctuation URL character if followed by a non-punctuation URL character
            ['.,;:!?)]                            # Punctuation URL character
            [\\w\\x80-\\xff#%\\~/@\\[\\]*(+=&$-]++ # Non-punctuation URL character
        )*
    )
    (\)?)                                                  # 3: Trailing closing parenthesis (for parethesis balancing post processing)
~xS
~

([\\s(好的,我认为这里的问题是您的正则表达式假设url前面有空格或标点符号,如第一行的注释中所述。如果您希望正则表达式在文本中匹配url而不带任何分隔符,我会删除第一行并按您的尝试更改方案部分,但在非捕获gr中不使用+量词oup
(?:https?| ftp)://

删除
+
。不清楚当前行为是什么以及预期的行为。@revo此正则表达式的当前结果是:
testhttp://google.com
。应该是
http://google.com
显示较大的示例输入。输入为“
你好worldhttp://google.com
。需要编辑正则表达式以仅匹配
http://google.com
。当前匹配为
testhttp://google.com