PHP 正则表达式截取HTML
来源:互联网 发布:java大量数据处理调优 编辑:程序博客网 时间:2024/06/17 08:26
用php的正则表达式相关函数,实现提取html超链接<a href="地址"></a>中的地址。
代码如下 复制代码
<?php
$preg='/<a .*?href="(.*?)".*?>/is';
$str ='<a href="链接1">URLNAME</a>文本段1<a href="链接2" target="_blank">URLNAME</a>文本段2<a target="_blank" href="链接3">URLNAME</a>...文本段n';
preg_match_all($preg,$str,$match);//在$str中搜索匹配所有符合$preg加入$match中
for($i=0;$i<count($match[1]);$i++)//逐个输出超链接地址
{
echo $match[1][$i]."<br />";
}
?>
最终输出:
链接1<br />链接2<br />链接3<br />
附一个
PHP的正则表达式提取图片地址的代码。
代码如下 复制代码
$str='<p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; line-height: 200%;"><img border="0" src="upfiles/2009/07/1246430143_4.jpg" alt=""/></p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; line-height: 200%;"><img border="0" src="upfiles/2009/07/1246430143_3.jpg" alt=""/></p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; line-height: 200%;"><img border="0" src="upfiles/2009/07/1246430143_1.jpg" alt=""/></p>';
$pattern="/<[img|IMG].*?src=['|"](.*?(?:[.gif|.jpg]))['|"].*?[/]?>/";
preg_match_all($pattern,$str,$match);
print_r($match);
代码如下 复制代码
<?php
$preg='/<a .*?href="(.*?)".*?>/is';
$str ='<a href="链接1">URLNAME</a>文本段1<a href="链接2" target="_blank">URLNAME</a>文本段2<a target="_blank" href="链接3">URLNAME</a>...文本段n';
preg_match_all($preg,$str,$match);//在$str中搜索匹配所有符合$preg加入$match中
for($i=0;$i<count($match[1]);$i++)//逐个输出超链接地址
{
echo $match[1][$i]."<br />";
}
?>
最终输出:
链接1<br />链接2<br />链接3<br />
附一个
PHP的正则表达式提取图片地址的代码。
代码如下 复制代码
$str='<p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; line-height: 200%;"><img border="0" src="upfiles/2009/07/1246430143_4.jpg" alt=""/></p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; line-height: 200%;"><img border="0" src="upfiles/2009/07/1246430143_3.jpg" alt=""/></p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; line-height: 200%;"><img border="0" src="upfiles/2009/07/1246430143_1.jpg" alt=""/></p>';
$pattern="/<[img|IMG].*?src=['|"](.*?(?:[.gif|.jpg]))['|"].*?[/]?>/";
preg_match_all($pattern,$str,$match);
print_r($match);
0 0
- PHP 正则表达式截取HTML
- 正则表达式 截取html中的 : [/w/W]+?
- php过滤html 正则表达式
- 正则表达式截取字符串
- 正则表达式截取字符串
- 正则表达式截取字符串
- php 正则表达式 html中的回车
- PHP正则表达式替换所有HTML标签
- php过滤html标签正则表达式
- PHP截取HTML字符串
- PHP截取html文章
- php 截取HTML字符串
- 通过正则表达式截取字符串
- 正则表达式截取成对括号
- 截取URL的正则表达式
- 用正则表达式截取字符串
- java正则表达式截取字符串
- 正则表达式截取URL参数
- DFS应用——查找强分支
- Linux内存管理原理
- Android PathMeasure使用
- [ecshop 资料] ectouch 微信通 微信接入 自动注册 的代码结构
- 如何从UIImage或者CGImage获取到图片的像素数据
- PHP 正则表达式截取HTML
- ios9 支付宝支付跳网页不跳 客户端
- Ubuntu安装切换jdk版本
- cascadeclassifier.cpp
- jar/war/ear文件的区别
- K-均值算法
- 时间复杂度
- 隐藏tabbar之后在原位置新建View不响应点击事件的方法
- 虚拟机——vnc