① php正則表達式替換圖片地址
<?php
/*PHP正則提取圖片img標記中的任意屬性*/
$str
=
'<center><img
src="/uploads/images/20100516000.jpg"
height="120"
width="120"><br
/>PHP正則提取或更改圖片img標記中的任意屬性</center>';
//1、取整個圖片代碼
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標記中的任意屬性*/
//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中 ,在資料庫中有一個文章表,現在我把文章查出來,怎麼通過正則表達式獲取文章里圖片的地址
正則也不是說隨便用的啊,起碼要看你圖片的鏈接地址是在哪裡啊
③ php 正則表達式怎麼把圖片URL匹配出來呢
使用preg_match_all函數,即可實現你的要求。代碼如下:
$str='<imgdatasrc="http://mm..com/mmbiz/2ItUdTx3iamOFK8QVqofnQ/640?tp=webp"data-s="300,640"data-ratio="0.625"data-w="400"style="box-sizing:border-box!important;width:auto!important;word-wrap:break-word!important;visibility:visible!important;"/>';
$pattern='/<img.*src="(.*?)"/';
preg_match_all($pattern,$str,$matches);
echo$matches[1][0];
//返回:http://mm..com/mmbiz/2ItUdTx3iamOFK8QVqofnQ/640?tp=webp
④ php如何用正則表達式抓取網頁中的圖片地址
當前標簽的屬性為:img(圖片)
標簽名稱恭伐多和鼙古俄汰藩咯:Image3
圖片地址是:index_files/dh_01.jpg
圖片的寬:95,高:32。
標簽的鏈接:index.asp
⑤ php如何使用正則表達式匹配url圖片啊
$image='<imgsrc="/avatar/100/r6s1g11.jpg"/>';
preg_match('/src="(.*?(jpg|jpeg|gif|png))/',$image,$url);
echo$url[1];
網頁上的圖片都是使用IMG標簽載入的,所以在匹配的時候是以src=" 開始匹配,很多人會覺得為什麼不是以 http:// 開始匹配,那是因為圖片地址會有相對地址(如:"/xxx/xxx.jpg")和絕對地址(如:"http://www.xxx.com/xxx/xxx.jpg")兩種情況,相對地址是沒有http:// 的,所以為了這兩種情況都能匹配,故以src=" 是最好的。
".*" 是匹配多個字元的意思,?號的意思是只匹配到第一個出現的jpg或jpeg、gif、png。
因為圖片有很多格式類型,所以用"(jpg|jpeg|gif|png)"來匹配多個類型,"|"是或者的意思。
⑥ 請問怎樣用php 正則表達式取設置寬和高的[img][/img]標簽裡面的圖片地址
用php給你運行了一個
$txt='[img=442,296]圖片地址1[/img]
[img=300,188]圖片地址2[/img]
[img=120,206]圖片地址3[/img]';
$re='/[img=(d+,d+)](S+?)[/img]/';//這里修改下,加上一個?防止以單行文本導致的定界符不準問題
$arr=[];
preg_match_all($re,$txt,$arr);
var_mp($arr);
運行結果如下
phptest.php
array(3){
[0]=>
array(3){
[0]=>
string(32)"[img=442,296]圖片地址1[/img]"
[1]=>
string(32)"[img=300,188]圖片地址2[/img]"
[2]=>
string(32)"[img=120,206]圖片地址3[/img]"
}
[1]=>
array(3){
[0]=>
string(7)"442,296"
[1]=>
string(7)"300,188"
[2]=>
string(7)"120,206"
}
[2]=>
array(3){
[0]=>
string(13)"圖片地址1"
[1]=>
string(13)"圖片地址2"
[2]=>
string(13)"圖片地址3"
}
}
//增加一個矩陣轉換
$txt='[img=442,296]圖片地址1[/img][img=300,188]圖片地址2[/img][img=120,206]圖片地址3[/img][img=120,206]wwww[/img]';
$re='/[img=(d+,d+)](S+?)[/img]/';
var_mp(preg_match_all_to_array($re,$txt));
functionpreg_match_all_to_array($re,$txt)
{
$arrs=[];
preg_match_all($re,$txt,$arrs);
if($arrs===false)
return$arrs;
//移除到總匹配數據
array_shift($arrs);
$return=[];
//獲取矩陣縱長
$arrs_longitudinal=count($arrs);
for($i=0;$i<$arrs_longitudinal;$i++){
//獲取單列橫長
$arrs_transverse=count($arrs[$i]);
for($j=0;$j<$arrs_transverse;$j++){
$return[$j][$i]=$arrs[$i][$j];
unset($arrs[$i][$j]);
}
unset($arrs[$i]);
}
return$return;
}
⑦ php中用正則表達式獲取html中所有圖片網址
<?php
$html='你的html代碼';
preg_match_all('/s+srcs?=s?['|"]([^'|"]*)/is',$html,$Array);
print_r($Array);
//$Array就是你想要的數組
⑧ 如何用正則表達式修改圖片路徑
其實不用正則表達式,用簡單的IF語句就可以
首先把圖片地址賦值到一個變數,然後利用explode函數通過第一個"/",截取域名,然後判斷域名是否跟你指定的一樣,然後根據判斷進行下一步工作就可以..
都說用IF語句就可以
$url
=
'pic.abc.com';
//你指定的域名.
..........
通過explode函數截取圖片地址
........
//判斷
if($url
==
$reurl){
//reurl是指截取圖片地址後的域名
$img
=
"PDE".$img;
}
//後面的就是不包含的情況,自己看情況寫..
⑨ 求php中正則表達式從html代碼中獲取圖片路徑
<?php
$test = '<p>444<img height="768" width="1024" alt="" src="/uploadfiles/28/Tree.jpg" /></p>
<p>444<img height="768" width="1024" alt="" src="/uploadfiles/sf/Tree.jpg" /></p>
fsdafasdfasdfasdf
<p>444<img height="768" width="1024" alt="" src="/uploadfiles/28/elm.jpg" /></p>
sdfasdfasdf<p>
<p>444<img height="768" width="1024" alt="" src="/uploadfiles/28/maple.jpg" /></p>
sdf32414撒旦發是否
<p>444<img height="768" width="1024" alt="" src="/uploadfiles/40/Tree.jpg" /></p>';
preg_match_all("/<p>.*src=\"([^^]*?)\".*<\/p>/i",$test,$match);
print_r($match[1]);
?>
這樣應該可以,我試的多行的,中間還夾雜一些字元,沒什麼問題,呵呵