php正则操作img中任意属性(取属性,正则替换等)
需要为固定新闻页图片的大小并且要加链接,首页把新页实现代码贴出来:
$as_message = preg_replace(\'/<img.+src=\"?(.+\.(jpg|gif|bmp|bnp|png))\"?.+>/i\', "<a href=\"$1\" target=_blank title=点击放大图片><img src=\"$1\" border=0 ;screen.width-600)this.width=screen.width-600\" return bbimg(this)\" ></a>", $as_message);
$as_message = preg_replace(\'/<IMG.+SRC=\"?(.+\.(jpg|gif|bmp|bnp|png))\"?.+>/i\', "<a href=\"$1\" target=_blank title=点击放大图片><img src=\"$1\" border=0 ;screen.width-600)this.width=screen.width-600\" return bbimg(this)\" ></a>", $as_message);
?>
另一种固定图片的js方法:
<tr>
<td> </td>
</tr>
</table>
<script>
var abc=document.getElementById("abc");
var imgs=abc.getElementsByTagName("img");
for (var i=0,g;g=imgs[i];i++)
g.onload=function(){if (this.width>600){this.width=600}else{if (this.height>600)this.height=600}}
</script>
数据库内容字段中存储的是原图的路径(当然还有其他文字内容啦,内容里插图时,存的是图片路径),但前台想使用缩略图,以下是网上找到的详细解决方法,参考其解决了我的问题。
<?php
/*
正则取图片img标记中的任意属性
搜集整理 脚本学堂 http://www.jbxue.com
*/
$word = \'<p height="22" align="cenetr">111 22</p> <img height="60" src="/upload/images/aaa.jpg" width=100 style=><div style="float:left;">中国人</div>\';
//取width
preg_match(\'/<img.+(width=\"?\d*\"?).+>/i\',$word,$matches);
echo $matches[1];
//取height
preg_match(\'/<img.+(height=\"?\d*\"?).+>/i\',$word,$matches);
echo $matches[1];
//取src
preg_match(\'/<img.+src=\"?(.+\.(jpg|gif|bmp|bnp|png))\"?.+>/i\',$word,$matches);
echo $matches[1];
/*正则替换去掉或改变图片img标记中的任意属性*****/
$str = \'<p height="22" align="cenetr">111 22</p> <img height="60" src="/upload/images/aaa.jpg" width=100 style=><div style="float:left;">中国人</div>
<p height="22" align="cenetr">31313 224344</p> <img src="/upload/images/bbb.jpg" height="60" width=100 style=><div style="float:left;">1212121</div>\';
//改变src属性(此处将原来的src="/upload/images/bbb.jpg"改变为src="/upload/_thumbs/Images/bbb.jpg")
print preg_replace(\'/(<img.+src=\"?.+)(images\/)(.+\.(jpg|gif|bmp|bnp|png)\"?.+>)/i\',"\${1}_thumbs/Images/\${3}",$str);
/*改变src属性,
此处将原来的src="/upload/images/bbb.jpg"改变为src="/upload/_thumbs/Images/bbb.jpg",并舍弃宽和高
(比如你想在前台显示缩略图,但数据库中存储的是原图的路径。为什么要舍弃宽高??你缩略图啊!还是原图的宽高,会怎样???)
*/
print preg_replace(\'/(<img).+(src=\"?.+)images\/(.+\.(jpg|gif|bmp|bnp|png)\"?).+>/i\',"\${1} \${2}_thumbs/Images/\${3}>",$str);
?>
您可能感兴趣的文章:
php正则表达式完全教程六
php正则表达式完全教程五
php正则表达式完全教程四
php正则表达式完全教程三
php正则表达式完全教程二
php正则表达式完全教程一
您可能感兴趣的文章:
PHP正则提取或替换img标记属性的实例代码
php正则操作img中任意属性(取属性,正则替换等)
php和js提取img标签的src属性值的正则表达式
php正则取img标记中alt src width heigh属性
Python re正则表达式模块及其用法
[javascript 学习笔记] 1. 面向对象
php正则表达式过滤html标签属性
网页标准研究:属性alt和title详细介绍
常用正则表达式全集
学习javascipt的正则表达式