用于捕获这些类型的网址的REGEX

时间:2016-04-07 21:09:11

标签: php sql regex url

我正在尝试制作一个正则表达式,它将捕获任何以http或https开头的url,并以jpg / png / gif结尾。正在对数据库进行SQL调用,该数据库返回帖子的未烹饪文本。

未烹饪的代码:

$ row ['rawtopic'] =“XYZ TESTING PURPOSES +                                                                                                                                                                                                                      +  http://i.imgur.com/filler_data.jpg +                                                                                                                                                                                                                      +  我们现在......“;

正则表达式:

$ output_2 = preg_match_all('/(https?:// [^] +?(?:。jpg | .png | .gif)$)/ i',$ row ['rawtopic'],$ match) ;

当我检查$ matches [0]中的输出时,$匹配[0] [0]和$ output_2,我没有得到任何返回,即没有匹配。我已经测试过以确保正则表达式在https://regex101.com/#pcre中工作,以及用实际的URL本身替换$ row ['rawtopic']。

有没有人对这可能是什么有任何想法?

1 个答案:

答案 0 :(得分:1)

你的正则表达式中有一些错误。如下所示进行更改:

// I've added one more url into the string (just for test)
$row['rawtopic'] = "XYZ TESTING PURPOSES + + http://i.imgur.com/filler_data.jpg + + We are currently... https://i.imgur.com/filler_data_test.jpg";

preg_match_all( '/\bhttps?:\/\/[^\s]+?\.(jpg|png|gif)\b/iu', $row['rawtopic'], $matches);

print_r($matches[0]);

输出:

Array
(
    [0] => http://i.imgur.com/filler_data.jpg
    [1] => https://i.imgur.com/filler_data_test.jpg
)