PHP正则提取或替换img标记属性

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
 
/*PHP正则提取图片img标记中的任意属性*/
$str = '<center><img src="/uploads/images/20100516000.jpg" height="120" width="120"><br />PHP正则提取或更改图片img标记中的任意属性</center>';
 
//1、取整个图片代码
preg_match('/<\s*img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i',$str,$match);
echo $match[0];
 
//2、取width
preg_match('/<img.+(width=\"?\d*\"?).+>/i',$str,$match);
echo $match[1];
 
//3、取height
preg_match('/<img.+(height=\"?\d*\"?).+>/i',$str,$match);
echo $match[1];
 
//4、取src
preg_match('/<img.+src=\"?(.+\.(jpg|gif|bmp|bnp|png))\"?.+>/i',$str,$match);
echo $match[1];
 
/*PHP正则替换图片img标记中的任意属性*/
//1、将src="/uploads/images/20100516000.jpg"替换为src="/uploads/uc/images/20100516000.jpg")
print preg_replace('/(<img.+src=\"?.+)(images\/)(.+\.(jpg|gif|bmp|bnp|png)\"?.+>)/i',"\${1}uc/images/\${3}",$str);
echo "<hr/>";
 
//2、将src="/uploads/images/20100516000.jpg"替换为src="/uploads/uc/images/20100516000.jpg",并省去宽和高
print preg_replace('/(<img).+(src=\"?.+)images\/(.+\.(jpg|gif|bmp|bnp|png)\"?).+>/i',"\${1} \${2}uc/images/\${3}>",$str);
 
 
 
?>
 /*
*获取html指定标签的相关属性
*@param string $content 要解析HTML内容
*@param string $attr 指定要获取的标签属性
*@param string $tag 指定解析标签
*@return array
*/ function get_html_attr_by_tag($content="",$attr="src",$tag="img"){ $arr=array(); $cache_arr=array(); $attr=explode(',',$attr); $tag=explode(',',$tag); foreach($tag as $i=>$t){ foreach($attr as $a){ preg_match_all("/<\s*".$t."\s+[^>]*?".$a."\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i",$content,$match); foreach($match[2] as $n=>$m){ $arr[$t][$n][$a]=$m; } } } return $arr;//array }

/**获取html指定标签的相关属性*@param string $content 要解析HTML内容*@param string $attr 指定要获取的标签属性*@param string $tag  指定解析标签*@return  array */
function get_html_attr_by_tag($content="",$attr="src",$tag="img"){$arr=array();$cache_arr=array();$attr=explode(',',$attr);$tag=explode(',',$tag);foreach($tag as $i=>$t){foreach($attr as $a){    preg_match_all("/<\s*".$t."\s+[^>]*?".$a."\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i",$content,$match);foreach($match[2] as $n=>$m){$arr[$t][$n][$a]=$m;    }}}return $arr;//array}

PHP 正则表达式匹配 img ,PHP 正则提取或替换图片 img 标记中的任意属性。的更多相关文章

  1. PHP 提取图片img标记中的任意属性

    PHP 提取图片img标记中的任意属性的简单实例. 复制代码代码如下: <?php /* PHP正则提取图片img标记中的任意属性 */ $str = '<center><im ...

  2. 解析PHP正则提取或替换img标记属性

    <?php/*PHP正则提取图片img标记中的任意属性*/$str = '<center><img src="/uploads/images/20100516000. ...

  3. PHP正则提取或替换img标记属性实现文章预览

    今天在想如何实现文章预览时,如果文章里面包含照片,那么就选取第一张照片作为预览图,如果没有照片,则截取文章的头150个字作为预览文字,但是因为保存在数据库的文章都是以富文本的形式,没办法直接提取,在网 ...

  4. PHP正则提取或替换img标记属性

    <?php   /*PHP正则提取图片img标记中的任意属性*/ $str = '<center><img src="/uploads/images/20100516 ...

  5. php正则提取html图片(img)src地址与任意属性的方法

    <?php /*PHP正则提取图片img标记中的任意属性*/ $str = '<center><img src="/uploads/images/2017020716 ...

  6. 一个通用的php正则表达式匹配或检测或提取特定字符类

      在php开发时,日常不可或缺地会用到正则表达式,可每次都要重新写,有时忘记了某一函数还要翻查手册,所以,抽空写了一个关于日常所用到的正则表达式区配类,便于随便移置调用.(^_^有点偷懒). /*/ ...

  7. php正则表达式匹配img中任意属性的方法

    经常和图片打交道,不得不用到一些提取图片中scr.alt.title.等的属性,这里总结给大家一些常用的,感觉还不错,比较通用! PHP正则表达式匹配img中任意属性PHP 复制代码代码如下: < ...

  8. JavaScript——正则匹配、正则提取、正则替换

    正则匹配 // 匹配日期 var dateStr = '2015-10-10'; var reg = /^\d{4}-\d{1,2}-\d{1,2}$/ console.log(reg.test(da ...

  9. VIM 用正则表达式,非贪婪匹配,匹配竖杠,竖线, 匹配中文,中文正则,倒数第二列, 匹配任意一个字符 :

    VIM 用正则表达式 批量替换文本,多行删除,复制,移动 在VIM中 用正则表达式 批量替换文本,多行删除,复制,移动 :n1,n2 m n3     移动n1-n2行(包括n1,n2)到n3行之下: ...

随机推荐

  1. ZZ:Solaris 10 软件包分析

    ZZ:Solaris 10 软件包分析 http://blog.chinaunix.net/uid-22759617-id-276756.html # Last updated: 2006-02-14 ...

  2. Java GC策略

    本文转自https://blog.csdn.net/rabbit_in_android/article/details/50386954 内存管理和垃圾回收 JVM内存组成结构 JVM栈由堆.栈.本地 ...

  3. 2017多校第6场 HDU 6096 String AC自动机

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6096 题意:给了一些模式串,然后再给出一些文本串的不想交的前后缀,问文本串在模式串的出现次数. 解法: ...

  4. WiderFace标注格式转PASCAL VOC2007标注格式

    #coding=utf-8 import os import cv2 from xml.dom.minidom import Document def create_xml(boxes_dict,ta ...

  5. .net连接sql server的几种连接字符串的写法

    .net连接sql server的几种连接字符串的写法 1, 混合验证模式登录 server=电脑名 或 电脑IP;database=数据库名;uid=数据库登录名;password=数据库登录密码 ...

  6. SQL 列 转换成 查询出来的 行

    查询  每个学生 的  (姓名,语文,数学,英语,成绩)为列 表结构如下: student: 学生表 grade 成绩表 : 查询出如下效果: SQL如下: select s.name,a.* fro ...

  7. Distinct Subsequences ——动态规划

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  8. shell读取nginx配置文件中nginx的端口

    #!/bin/shport=`nl /usr/local/openresty/nginx/conf/nginx.conf | sed -n '/listen/p' | awk 'NR==1{print ...

  9. Centos查找大文件的办法

    find / -size +100M -exec ls -lh {} \; # 查看整体磁盘占用df -h #切换到这块磁盘检查一下这块磁盘的哪个文件夹占用高,再逐层去查找 du -h --max-d ...

  10. Centos7Yum安装配置指定版本nginx

    1.安装 rpm -ivh http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.14.2-1.el7_4.ngx.x86_64.rpm 2.启 ...