Home > Backend Development > PHP Tutorial > PHP regular operation of any attribute in img (taking attributes, regular replacement, etc.)

PHP regular operation of any attribute in img (taking attributes, regular replacement, etc.)

WBOY
Release: 2016-07-25 09:00:05
Original
1098 people have browsed it
  1. $as_message = preg_replace('//i', "", $as_message);
  2. $as_message = preg_replace('//i', "", $as_message);
  3. ?>
复制代码

另一种固定图片的js方法:

  1.  
  2. <script></li> <li>var abc=document.getElementById("abc");</li> <li>var imgs=abc.getElementsByTagName("img");</li> <li>for (var i=0,g;g=imgs[i];i++)</li> <li>g.onload=function(){if (this.width>600){this.width=600}else{if (this.height>600)this.height=600}}</li> <li></script>
复制代码

数据库内容字段中存储的是原图的路径(当然还有其他文字内容啦,内容里插图时,存的是图片路径),但前台想使用缩略图,以下是网上找到的详细解决方法,参考其解决了我的问题。

  1. /*

  2. 正则取图片img标记中的任意属性
  3. 搜集整理 程序员之家 http://bbs.it-home.org
  4. */
  5. $word = '

    111 22

    中国人
    ';
  6. //取width
  7. preg_match('//i',$word,$matches);
  8. echo $matches[1];

  9. //取height

  10. preg_match('//i',$word,$matches);
  11. echo $matches[1];

  12. //取src

  13. preg_match('//i',$word,$matches);
  14. echo $matches[1];

  15. /*正则替换去掉或改变图片img标记中的任意属性*****/

  16. $str = '

    111 22

    中国人
  17. 31313 224344

    1212121
    ';

  18. //改变src属性(此处将原来的src="/upload/images/bbb.jpg"改变为src="/upload/_thumbs/Images/bbb.jpg")

  19. print preg_replace('/()/i',"$左手写情_thumbs/Images/${3}",$str);

  20. /*改变src属性,

  21. 此处将原来的src="/upload/images/bbb.jpg"改变为src="/upload/_thumbs/Images/bbb.jpg",并舍弃宽和高
  22. (比如你想在前台显示缩略图,但数据库中存储的是原图的路径。为什么要舍弃宽高??你缩略图啊!还是原图的宽高,会怎样???)
  23. */
  24. print preg_replace('/(/i',"$左手写情 ${2}_thumbs/Images/${3}>",$str);
  25. ?>

复制代码


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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template