php正则提取html图片(img)src地址与任意属性
2016-03-21 11:17
921 查看
http://www.smsyun.com/home-index-page-id-134.html
简单版:
详细版:
简单版:
01 | <?php |
02 | header( "Content-Type:text/html;charset=utf-8" ); |
03 |
04 | $str ='<div class = "ui-block-a" align= "center" > |
05 | <ahref= "online-39.html" ><img class = "lazy" width= "131" height= "177" src= " /> |
06 | <h3>2014年</h3></a> |
07 | </div>'; |
08 |
09 | $imgpreg = "/<img(.*?)src=\"(.+?)\".*?>/" ; |
10 | preg_match( $imgpreg , $str , $img ); |
11 | $mycount = count ( $img )-1; |
12 | $imgval = $iim [ $mycount ]; |
13 | if (! empty ( $imgval )){ |
14 | echo $imgval ; |
15 | } else { |
16 | echo 'no' ; |
17 | } |
18 | ?> |
01 | <?php |
02 | /*PHP正则提取图片img标记中的任意属性*/ |
03 | $str = '<center><imgsrc="/uploads/images/20100516000.jpg"height="120"width="120"><br/>PHP正则提取或更改图片img标记中的任意属性</center>' ; |
04 |
05 | //1、取整个图片代码 |
06 | preg_match( '/<\s*img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i' , $str , $match ); |
07 | echo $match [0]; |
08 |
09 | //2、取width |
10 | preg_match( '/<img.+(width=\"?\d*\"?).+>/i' , $str , $match ); |
11 | echo $match [1]; |
12 |
13 | //3、取height |
14 | preg_match( '/<img.+(height=\"?\d*\"?).+>/i' , $str , $match ); |
15 | echo $match [1]; |
16 |
17 | //4、取src |
18 | preg_match( '/<img.+src=\"?(.+\.(jpg|gif|bmp|bnp|png))\"?.+>/i' , $str , $match ); |
19 | echo $match [1]; |
20 |
21 | /*PHP正则替换图片img标记中的任意属性*/ |
22 | //1、将src="/uploads/images/20100516000.jpg"替换为src="/uploads/uc/images/20100516000.jpg") |
23 | printpreg_replace( '/(<img.+src=\"?.+)(images\/)(.+\.(jpg|gif|bmp|bnp|png)\"?.+>)/i' ,"\${1}uc/images/\${3}", $str ); |
24 | echo "<hr/>" ; |
25 |
26 | //2、将src="/uploads/images/20100516000.jpg"替换为src="/uploads/uc/images/20100516000.jpg",并省去宽和高 |
27 | printpreg_replace( '/(<img).+(src=\"?.+)images\/(.+\.(jpg|gif|bmp|bnp|png)\"?).+>/i' ,"\${1}\${2}uc/images/\${3}>", $str ); |
28 | ?> |
相关文章推荐
- dedecms中的{dede:php}{/dede:php}的详细用法
- DEDECMSv57在PHP5.4不能正常运行的解决方法
- php上传文件超时办法。把下面的4个值都调整大一些
- 用PHP读取超大文件的实例代码
- 这篇文章主要介绍了PHP整合七牛实现上传文件的相关资料,需要的朋友可以参考下
- 完美解决PHP中的Cannot modify header information 问题
- php中实现精确设置session过期时间的方法
- thinkphp去掉index.php访问后缀
- PHP中file_exists与is_file,is_dir的区别
- PHP不用数据库怎么弄图片分页
- 淘宝IP地址库API接口 (PHP)通过ip获取地址信息
- 用PHP对文件夹进行访问权限设置(文件防止被下载设置)
- 图片处理神器ImageMagick以及PHP的imagick扩展
- php的$_FILES的临时储存文件与回收机制
- PHP 获取指定URl页面中所有链接
- Notice: Undefined offset: 1 in D:\\wwwroot\\wr\\askseo\\404.php on line 5
- php单引号中字符串的转义(\"\\\")
- PHP 限制国内IP访问 代码实例
- 解析PHP默认的session id生成算法
- 【转载】PHP5 session 详解【经典】