php正則操作img中任意屬性(取屬性,正則替換等)

來源:互聯網
上載者:User
  1. $as_message = preg_replace(\'//i\', "", $as_message);
  2. $as_message = preg_replace(\'//i\', "", $as_message);
  3. ?>
複製代碼

另一種固定圖片的js方法:

複製代碼

資料庫內容欄位中儲存的是原圖的路徑(當然還有其他文字內容啦,內容裡插圖時,存的是圖片路徑),但前台想使用縮圖,以下是網上找到的詳細解決方案,參考其解決了我的問題。

  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. ?>

複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.