PHP正则匹配到2个字符串之间的内容,匹配HTML便签内容

PHP正则匹配到2个字符串之间的内容

$preg= '/xue[\s\S]*?om/i';
preg_match_all($preg,"学并思网址xuebingsi.com",$res);
var_dump($res[0][0]);

匹配HTML便签内容

假如需要匹配下面HTML字符串里面的第三个<img> 把它替换掉,或者删除掉这样个需求

$string ='<p>

<img src="http://md-bot-service.oss-cn-shenzhen.aliyuncs.com/web/uploads/image/89/89b2bc7a3281c80c6f9274933c4cac02167d9f0f.jpg?x-oss-process=image/quality,q_85" title="" alt=""/>

<img src="http://md-bot-service.oss-cn-shenzhen.aliyuncs.com/web/uploads/image/96/96311b2febc784728aa07fe3f9f10b7f7504c4f4.jpg?x-oss-process=image/quality,q_85" title="" alt=""/>

<img src="http://md-bot-service.oss-cn-shenzhen.aliyuncs.com/web/uploads/image/d4/d439d3497dea212b14349064e313ab0b82ce8a99.png?x-oss-process=image/quality,q_85" title="" alt=""/>

</p>

<p><img src="http://md-bot-service.oss-cn-shenzhen.aliyuncs.com/web/uploads/image/da/da56b60d205b620a572dc0be0a72b81fc9ddf946.jpg?x-oss-process=image/quality,q_85"/></p>

<p><img src="http://md-bot-service.oss-cn-shenzhen.aliyuncs.com/web/uploads/image/fa/faa15afc625a7fe2d754a3b0aa6b5cf0b8992740.jpg?x-oss-process=image/quality,q_85"/></p>

<p><img src="http://md-bot-service.oss-cn-shenzhen.aliyuncs.com/web/uploads/image/5e/5ec941b41b5119d97c24e23a4f8b8785cc8be8ae.jpg?x-oss-process=image/quality,q_85"/></p>

<p><img src="http://md-bot-service.oss-cn-shenzhen.aliyuncs.com/web/uploads/image/94/946580ab9105ea1632301f0b3890c41b510bab35.jpg?x-oss-process=image/quality,q_85"/></p>

<p><br/></p>';

------------------------------------------------------------------------------------------

$pattern = '/<img src=\"http:\/\/md-bot-service.oss-cn-shenzhen.aliyuncs.com\/web\/uploads\/image\/d4\/d439d3497dea212b14349064e313ab0b82ce8a99\.png([\s\S])*?/i';   // '/([\s\S])*?/i'

$replacement = '';

//正则匹配替换函数
preg_replace($pattern, $replacement, $string);//$pattern 被替换的内容, $replacement替换内容(我这里为空表示删除,你们按照实际情况使用)

posted @ 2018-08-15 15:13  罗马的路上  阅读(3682)  评论(0编辑  收藏  举报