一些在线的文本编辑器比如ueditor等,编辑和发表的文章是带有html标签的。开发中有时候需要从一些网站抓取文章中的图片,需要用到正则。
比如下面从一段文章里取出之中的img标签的src属性和图片的后缀类型:
<?php
header('Content-type:text/html;charset=UTF-8');
$str = <<<HEREDOC
<span style="line-height:35px"><span style="font-size:14px;"><a href="http://news.fx678.com/news/keywords/mg.shtml" style="text-decoration: none; font-family: 'Microsoft YaHei', sans-serif; color: rgb(15, 63, 148); font-size: 16px; line-height: 32px;" target="_blank">美国</a><span style="color: rgb(30, 30, 30); font-family: 'Microsoft YaHei', sans-serif; line-height: 32px;">NYMEX WTI</span><a href="http://news.fx678.com/news/keywords/oil.shtml" style="text-decoration: none; font-family: 'Microsoft YaHei', sans-serif; color: rgb(15, 63, 148); font-size: 16px; line-height: 32px;" target="_blank">原油</a><span style="color: rgb(30, 30, 30); font-family: 'Microsoft YaHei', sans-serif; line-height: 32px;">1月期货周一收涨0.01</span><a href="http://news.fx678.com/news/keywords/usd.shtml" style="text-decoration: none; font-family: 'Microsoft YaHei', sans-serif; color: rgb(15, 63, 148); font-size: 16px; line-height: 32px;" target="_blank">美元</a><span style="color: rgb(30, 30, 30); font-family: 'Microsoft YaHei', sans-serif; line-height: 32px;">,涨幅0.03%,报34.74美元/桶。WTI 1月原油期货到期交割前,投资者结清空头头寸,使得WTI 1月油价跌幅一度收窄。不过,</span><a href="http://news.fx678.com/news/keywords/quanqiu.shtml" style="text-decoration: none; font-family: 'Microsoft YaHei', sans-serif; color: rgb(15, 63, 148); font-size: 16px; line-height: 32px;" target="_blank">全球</a><span style="color: rgb(30, 30, 30); font-family: 'Microsoft YaHei', sans-serif; line-height: 32px;">油价基准之一的布伦特油价,周一则继续下跌,盘中更是创下来自2004年7月以来的新低。</span><br style="color: rgb(30, 30, 30); font-family: 'Microsoft YaHei', sans-serif; font-size: 16px; line-height: 32px;"><br style="color: rgb(30, 30, 30); font-family: 'Microsoft YaHei', sans-serif; font-size: 16px; line-height: 32px;"><img alt="【原油收盘】市场前景多空分化,美油收涨布油收跌" src="/uploads/allimg/151222/09412930F-0.jpg" style="padding: 0px; margin: 0px; border: 0px; outline: none; color: rgb(30, 30, 30); font-family: 'Microsoft YaHei', sans-serif; font-size: 16px; line-height: 32px; cursor: pointer;"><br style="color: rgb(30, 30, 30); font-fa