Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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/9/google-apps-script/6.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
URL正则表达式问题,php_Php_Regex - Fatal编程技术网

URL正则表达式问题,php

URL正则表达式问题,php,php,regex,Php,Regex,我有一个我使用的URL正则表达式(并且经常使用)。它很适合我查找各种URL格式和http协议。这就是说,如果一切都是在蒲公英乐园,我就不会在这里写作了 我遇到了一个小问题,这是我现在的正则表达式引起的 在搜索URL字符串时,如果字符串由类似示例…的内容组成,请参见它将作为URL处理。可以有任意数量的句点,但是它只提取最后一个句点之后的最后3个字符 有没有办法解决这个问题 例如: $string = "Here's a url, hello.com. But this...shouldn't sh

我有一个我使用的URL正则表达式(并且经常使用)。它很适合我查找各种URL格式和http协议。这就是说,如果一切都是在蒲公英乐园,我就不会在这里写作了

我遇到了一个小问题,这是我现在的正则表达式引起的

在搜索URL字符串时,如果字符串由类似
示例…的内容组成,请参见
它将作为URL处理。可以有任意数量的句点,但是它只提取最后一个句点之后的最后3个字符

有没有办法解决这个问题

例如:

$string = "Here's a url, hello.com. But this...shouldn't show.";

$url_regex = "/((https?|ftp)\:\/\/)?([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?([a-z0-9-.]*)\.([a-z]{2,3})(\:[0-9]{2,5})?(\/([a-z0-9+\$_\-~@\(\)\%]\.?)+)*\/?(\?[a-z+&\$_.-][a-z0-9;:@&#%=+\/\$_.-]*)?(#[a-z_.-][a-z0-9+\$_.-]*)?/i";

preg_match_all($url_regex, $string, $urls);

return $urls;

这里的问题是您在允许的字符内添加了句点,这意味着可能有多个连续的句点。另外,
\b
在处理内联搜索时也很重要

\b((https?|ftp)\:\/\/)?([a-z0-9+!*(),;?&=\$_-]+(\:[a-z0-9+!*(),;?&=\$_-]+)?@)?([a-z0-9-]*)\.([a-z]+){2,3}(\:[0-9])?(\/([a-z0-9+\$_\-~@\(\)\%]?)+)*\/?(\?[a-z+&\$_-][a-z0-9;:@&#%=+\/\$_-]*)?(#[a-z_-][a-z0-9+\$_-]*)?\b


编辑:更新答案以忽略匹配项,如示例。c

以下代码可解决您的问题。我这边有考试

$string = "Here's a url, hello.com. But this...shouldn't show.";

$url_regex = "/((https?|ftp)\:\/\/)?([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?([a-z0-9-]+?)\.([a-z]{2,3})(\:[0-9]{2,5})?(\/([a-z0-9+\$_\-~@\(\)\%]\.?)+)*\/?(\?[a-z+&\$_.-][a-z0-9;:@&#%=+\/\$_.-]*)?(#[a-z_.-][a-z0-9+\$_.-]*)?/i";

preg_match_all($url_regex, $string, $urls);
在字符串中使用带有URL的https和http。


谢谢正在测试中获取此错误
preg\u match\u all():分隔符不能是字母数字或反斜杠$(a-z0-9-9-0-10-10-10-10-10-10-9-9-10-9-9-9)))除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了上述的一个(a-z0-10-10-10-9-9-9-9-9-9手手手手手手手手手手手手手手手想想想想想想想想想想外,,,,,,,,,,;;;以及以及以及将将将将作为作为除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了上述上述上述-10-10-10-10-10-10?(#[a-z-][a-z0-9+\$\U-]*)?\b/i”;preg_match_all($url_regex,$string,$url);echo$url[0][0]打印
hello.com
由于某种原因,我无法在注释中键入多行代码…忘记了开头
/
和结尾
/I
:)啊!找到解决方案的兴奋之情……)抢手货我编辑了答案以忽略这些匹配。
$string = "this is my website http://example.com and this is my friend website https://pqr.com etc, this...shouldn't show";

$regex = '/\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i';

preg_match_all($regex, $string, $matches);

print_r($matches[0]);