我要匹配一篇文章内容里面的所有图片,于是我写了下面这样一个正则。
$pattern = "/<img.*src=['\"](.*?)\/?>/i";
preg_match_all($pattern, $content, $match);
echo '<pre>',$content;print_r($match);exit;
打印结果如下:
Array
(
[0] => Array
(
[0] => <img alt="在这里插入图片描述" src="https://img-blog.csdnimg.cn/65ff3683fe2c43c08b7afca44f172d59.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_Q1NETiBATWF0Y2hfaA==,size_21,color_FFFFFF,t_70,g_se,x_16" />
[1] => <img alt="在这里插入图片描述" src="https://img-blog.csdnimg.cn/f525cd92182e4538ad89950db120a70b.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_Q1NETiBATWF0Y2hfaA==,size_20,color_FFFFFF,t_70,g_se,x_16" />
[2] => <img alt="在这里插入图片描述" src="https://img-blog.csdnimg.cn/5791f45347e3487894bf31ebaffad50f.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_Q1NETiBATWF0Y2hfaA==,size_20,color_FFFFFF,t_70,g_se,x_16" />
[3] => <img alt="在这里插入图片描述" src="https://img-blog.csdnimg.cn/81e8867c2adf4c6aaf66fbe6935d89a8.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_Q1NETiBATWF0Y2hfaA==,size_47,color_FFFFFF,t_70,g_se,x_16" />
)
[1] => Array
(
[0] => https://img-blog.csdnimg.cn/65ff3683fe2c43c08b7afca44f172d59.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_Q1NETiBATWF0Y2hfaA==,size_21,color_FFFFFF,t_70,g_se,x_16"
[1] => https://img-blog.csdnimg.cn/f525cd92182e4538ad89950db120a70b.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_Q1NETiBATWF0Y2hfaA==,size_20,color_FFFFFF,t_70,g_se,x_16"
[2] => https://img-blog.csdnimg.cn/5791f45347e3487894bf31ebaffad50f.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_Q1NETiBATWF0Y2hfaA==,size_20,color_FFFFFF,t_70,g_se,x_16"
[3] => https://img-blog.csdnimg.cn/81e8867c2adf4c6aaf66fbe6935d89a8.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_Q1NETiBATWF0Y2hfaA==,size_47,color_FFFFFF,t_70,g_se,x_16"
)
)
我现在不知道如何清掉 $match[1] 结果里面的最后的双引号,我的想法是把正则改成如下:
$pattern = "/<img.*src=['\"](.*['\"]$)?\"\/?>/i";
想法就是src里面的那些任意字符,也就是 .* 这个排除双引号这个字符,现在这个写法是啥都匹配不到了,哈哈哈,还请大神帮忙指点一二,谢谢。
直接这样就好了
<img.*?\ssrc="([^"]+)"
补充内容(部分语言里面,
\1
可能要写作\\1
)