如何在php中保存正则表达式的结果

时间:2015-12-11 18:03:53

标签: php regex

我有一个包含图片文件地址的字符串:

<p><img alt="" src="/upload/images/image.jpg" style="width: 100px; height: 500px;" /></p>

我写了一个regural表达来找到这个地址。图像始终存储在/ upload / images /:

preg_match("/(\/upload\/images\/).+(.jpg)|(.jpeg)|(.png)|(.bmp)|(.gif)/", $digest['content']);

有没有办法保存preg_match找到的这个地址?

1 个答案:

答案 0 :(得分:2)

当然有 read the manual

  

int preg_match(string $ pattern,string $ subject [, array&amp; $ matches [,int $ flags = 0 [,int $ offset = 0]]])

<强>匹配

  

如果提供了匹配,那么它将填充搜索结果。 $ matches [0]将包含与完整模式匹配的文本,$ matches [1]将具有与第一个捕获的带括号的子模式匹配的文本,依此类推。

发送第3个参数,所有匹配都将包含在其中。

preg_match("/(\/upload\/images\/).+(.jpg)|(.jpeg)|(.png)|(.bmp)|(.gif)/", $digest['content'], $matches);
print_r($matches);