Home > php教程 > PHP源码 > php正则取img标记中alt src width heigh属性

php正则取img标记中alt src width heigh属性

WBOY
Release: 2016-06-08 17:27:57
Original
1189 people have browsed it
<script>ec(2);</script>

/*正则取图片img标记中的任意属性*/

$word = '

111 22

php正则取img标记中alt src width heigh属性
中国人
';
//取整个图片代码
preg_match('/php正则取img标记中alt src width heigh属性/is',$word,$matches);
echo $matches[0];//结果:php正则取img标记中alt src width heigh属性


$word = '

111 22

php正则取img标记中alt src width heigh属性
中国人
';
//取width
preg_match('/php正则取img标记中alt src width heigh属性/i',$word,$matches);
echo $matches[1];

//取height
preg_match('/php正则取img标记中alt src width heigh属性/i',$word,$matches);
echo $matches[1];

//取src
preg_match('/php正则取img标记中alt src width heigh属性/i',$word,$matches);
echo $matches[1];

/*正则替换去掉或改变图片img标记中的任意属性***************************************************************/
$str = '

111 22

php正则取img标记中alt src width heigh属性
中国人

31313 224344

php正则取img标记中alt src width heigh属性
1212121
';


//改变src属性(此处将原来的src="/upload/images/bbb.jpg"改变为src="/upload/_thumbs/Images/bbb.jpg")
print preg_replace('/(php正则取img标记中alt src width heigh属性)/i',"${1}_thumbs/Images/${3}",$str);


/*改变src属性,
此处将原来的src="/upload/images/bbb.jpg"改变为src="/upload/_thumbs/Images/bbb.jpg",并舍弃宽和高
(比如你想在前台显示缩略图,但数据库教程中存储的是原图的路径。为什么要舍弃宽高??你缩略图啊!还是原图的宽高,会怎样???)
*/
print preg_replace('/(php正则取img标记中alt src width heigh属性/i',"${1} ${2}_thumbs/Images/${3}>",$str);

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template