解析PHP正則提取或替換img標(biāo)記屬性
核心代碼
<?php /*PHP正則提取圖片img標(biāo)記中的任意屬性*/ $str = '<center><img src="/uploads/images/20100516000.jpg" height="120" width="120"><br />PHP正則提取或更改圖片img標(biāo)記中的任意屬性</center>'; //1、取整個(gè)圖片代碼 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標(biāo)記中的任意屬性*/ //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); ?>
PHP獲取所有圖片地址正則表達(dá)式
不帶圖片的所有圖片地址匹配:
preg_match_all('/(\s+src\s?\=)\s?[\'|"]([^\'|"]*)/is', $request->input('detail_content'), $match);
帶data:image/的:
preg_match_all('/(\s+src\s?\=)\s?[\'|"]([^\'|"]*)[data]/is', $request->input('detail_content'), $match);
PHP正則獲取一段字符串中所有圖片地址
有的時(shí)候我們需要獲取文章內(nèi)容或者字符串中所有的圖片地址,那么我們首先想到的就是正則匹配,該怎么實(shí)現(xiàn)呢,下面就是小編的親測(cè)方法
$str = '<p><img src="/upload/20180621/1529561322214.png" /></p><p><img src="/Home/images/404.jpg" style="" title="404.jpg"/></p><p><img src="/upload/20180621/1529561322214.png" style="" title="1529561322214.png"/></p><p><br/></p>'; $preg = '/<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/i';//匹配img標(biāo)簽的正則表達(dá)式 preg_match_all($preg, $str, $allImg);//這里匹配所有的img echo '<pre>'; print_r($allImg);
輸出結(jié)果如下
( [0] => Array ( [0] => <img src="/upload/20180621/1529561322214.png" /> [1] => <img src="/Home/images/404.jpg" style="" title="404.jpg"/> [2] => <img src="/upload/20180621/1529561322214.png" style="" title="1529561322214.png"/> ) [1] => Array ( [0] => /upload/20180621/1529561322214.png [1] => /Home/images/404.jpg [2] => /upload/20180621/1529561322214.png ) )
到此這篇關(guān)于解析PHP正則提取或替換img標(biāo)記屬性的文章就介紹到這了,更多相關(guān)PHP 正則提取 替換img標(biāo)記屬性內(nèi)容請(qǐng)搜素腳本之家以前的文章或下面相關(guān)文章,希望大家以后多多支持腳本之家!
相關(guān)文章
Opcache導(dǎo)致php-fpm崩潰nginx返回502
這篇文章主要介紹了Opcache導(dǎo)致php-fpm崩潰nginx返回502的解決方法,十分實(shí)用,需要的朋友可以參考下2015-03-03PHP實(shí)現(xiàn)json_decode不轉(zhuǎn)義中文的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)json_decode不轉(zhuǎn)義中文的方法,結(jié)合實(shí)例形式具體分析了php5.4+及5.3版本針對(duì)json_decode實(shí)現(xiàn)不轉(zhuǎn)義中文的具體操作技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-05-05php array_walk() 數(shù)組函數(shù)
函數(shù)array_walk():單一數(shù)組回調(diào)函數(shù)---對(duì)數(shù)組中的每個(gè)成員應(yīng)用用戶函數(shù)2011-07-07PHP合并數(shù)組函數(shù)array_merge用法分析
這篇文章主要介紹了PHP合并數(shù)組函數(shù)array_merge用法,結(jié)合實(shí)例形式分析了php數(shù)組合并函數(shù)array_merge的具體功能、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-02-02phpstorm 正則匹配刪除空行、注釋行(替換注釋行為空行)
這篇文章主要介紹了phpstorm 正則匹配刪除空行、注釋行,需要的朋友可以參考下2018-01-01