正则匹配问题

来源:互联网 发布:淘宝如何取消超级推广 编辑:程序博客网 时间:2024/06/13 08:45

昨天被一个正则难倒了两个小时,发现发现知识点一不用就会很快忘记。

使用场景:很多电商平台,经常把商品详情以HTML的形式存入到数据库字段中,然后无论做APP接口还是别的应用,取出来就拿去用比较方便,但是如果详情页需要变动,需要维护的商品详情页就会有很大的工作量,比如今年3.15过后,电商要求电子商务的商品需要在商品详情页加上价格说明图片在商品详情页,所以我是打算用正则来做这一部分,但是当正则匹配的时候出现了很多问题,因为好久不用正则,很生疏!!!!

商品详情字段存储的HTML:  <p><img src="/public/images/20170321/1490084253328704.jpg" style="" title="1490084253328704.jpg"/></p><p><img src="/public/images/20170321/1490084253537364.jpg" style="" title="1490084253537364.jpg"/></p><p><img src="/public/images/20170321/1490084253799043.jpg" style="" title="1490084253799043.jpg"/></p><p><img src="/public/images/20170321/1490084253123933.jpg" style="" title="1490084253123933.jpg"/></p><p>&nbsp; &nbsp; &nbsp; &nbsp;<br/>使用方法<br/><br/><br/> 注意事项<br/><br/><br/> 产品说明<br/><br/><br/> 商品特征<br/> &nbsp; &nbsp;<br/></p>  

基本就是p标签包围img标签的样子,最后一个包围的是参数之类的,我们需要把后台配置的价格说明图插入到参数说明之前,我的方法是匹配p标签的内容,然后替换参数部分,在把参数部分补足p标签加到替换之后的字符串的后面。代码实现如下:

$str = '<p><img src="/public/images/20170321/1490084253328704.jpg" style="" title="1490084253328704.jpg"/></p><p><img src="/public/images/20170321/1490084253537364.jpg" style="" title="1490084253537364.jpg"/></p><p><img src="/public/images/20170321/1490084253799043.jpg" style="" title="1490084253799043.jpg"/></p><p><img src="/public/images/20170321/1490084253123933.jpg" style="" title="1490084253123933.jpg"/></p><p>&nbsp; &nbsp; &nbsp; &nbsp;<br/>使用方法<br/><br/><br/> 注意事项<br/><br/><br/> 产品说明<br/><br/><br/> 商品特征<br/> &nbsp; &nbsp;<br/></p>';  <strong><span style="background-color: #ff0000;">preg_match_all('/<p>(.*?)<\/p>/', $str, $b);</span></strong>  $count = count($b[0]);  $c = str_replace($b[0][$count-1],'<p><img src="http://img4.imgtn.bdimg.com/it/u=3677832436,4238861421&fm=214&gp=0.jpg"></p>'.$b[0][$count-1],$str);  print_r($c);die;  
 preg_match_all('/<p>(.*?)<\/p>/', $str, $b);

注:默认模式下 PREG_PATTERN_ORDER
 如果匹配到的话 $p会产生一个二维数组

Array  (      [0] => Array          (              [0] => <p><img src="/public/images/20170321/1490084253328704.jpg" style="" title="1490084253328704.jpg"/></p>              [1] => <p><img src="/public/images/20170321/1490084253537364.jpg" style="" title="1490084253537364.jpg"/></p>              [2] => <p><img src="/public/images/20170321/1490084253799043.jpg" style="" title="1490084253799043.jpg"/></p>              [3] => <p><img src="/public/images/20170321/1490084253123933.jpg" style="" title="1490084253123933.jpg"/></p>              [4] => <p>&nbsp; &nbsp; &nbsp; &nbsp;<br/>使用方法<br/><br/><br/> 注意事项<br/><br/><br/> 产品说明<br/><br/><br/> 商品特征<br/> &nbsp; &nbsp;<br/></p>          )      [1] => Array          (              [0] => <img src="/public/images/20170321/1490084253328704.jpg" style="" title="1490084253328704.jpg"/>              [1] => <img src="/public/images/20170321/1490084253537364.jpg" style="" title="1490084253537364.jpg"/>              [2] => <img src="/public/images/20170321/1490084253799043.jpg" style="" title="1490084253799043.jpg"/>              [3] => <img src="/public/images/20170321/1490084253123933.jpg" style="" title="1490084253123933.jpg"/>              [4] => &nbsp; &nbsp; &nbsp; &nbsp;<br/>使用方法<br/><br/><br/> 注意事项<br/><br/><br/> 产品说明<br/><br/><br/> 商品特征<br/> &nbsp; &nbsp;<br/>          )  )  

b[0]b[1] 为(.*?)匹配到的内容,即为P标签内的内容;
 然后我们就可以通过原定计划 行事了。

0 0
原创粉丝点击