我有 2 个图像标签,一个接一个
<img class="c1 c2 c3" title="Image Title 1" src="http://example.com/image-1.jpg" alt="" width="620" height="521"><img class="c1 c2 c3" title="Image Title 2" src="http://example.com/image-2.jpg" alt="" width="620" height="521">
Run Code Online (Sandbox Code Playgroud)
我想要一个可以获取两件事的正则表达式:
我该怎么做?
PS有人知道我可以在哪里在线测试正则表达式吗
匹配第一个IMG标签及其src值的正则表达式:
$subject = '<img class="c1 c2 c3" title="Image Title 1" src="http://example.com/image-1.jpg" alt="" width="620" height="521"><img class="c1 c2 c3" title="Image Title 2" src="http://example.com/image-2.jpg" alt="" width="620" height="521">';
preg_match('/<img\s.*?\bsrc="(.*?)".*?>/si', $subject, $matches);
print_r($matches);
Run Code Online (Sandbox Code Playgroud)
输出:
Array
(
[0] => <img class="c1 c2 c3" title="Image Title 1" src="http://example.com/image-1.jpg" alt="" width="620" height="521">
[1] => http://example.com/image-1.jpg
)
Run Code Online (Sandbox Code Playgroud)
在线测试正则表达式的工具有很多。以下只是其中的一些: