㈠ 用php獲取鏈接及圖片路徑的方法
<?php
$str="Thisisatest.Thisisatest.Thisisa<ahref=http://link1.com><imgsrc=http://img1.jpg/></a>test.Thisisatest.Thisisatest. ".
"Thisisatest.Thisisatest.<ahref=http://link2.com><imgsrc=http://img2.jpg/></a>Thisisatest.Thisisatest.Thisisatest. ".
"<ahref=http://link3.com><imgsrc=http://img3.jpg/></a>";
$regex='/<as+href=(.*)s*><imgs+src=(.*)s*/></a>/';
$output=array();
if(preg_match_all($regex,$str,$matches)!==false){
if(isset($matches[1])&&isset($matches[2])){
$links=$matches[1];
$imgs=$matches[2];
foreach($linksas$key=>$link){
$img=isset($imgs[$key])?$imgs[$key]:'';
$output[]="<ahref="{$link}"><imgsrc="{$img}"/></a>";
}
}
}
var_mp($output);
㈡ 請問怎樣用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 preg_match_all()函數怎麼匹配文章中的所有圖片鏈接並列印出來
<?php
$Html=@file_get_contents('5.html');
$Html=preg_replace('/s{2,}| /i','',$Html);//過濾掉換行和2個以上的空格
preg_match_all('/<imgs+[^>]*srcs?=s?['|"]([^'|"]*)['|"]/i',$Html,$Image);
print_r($Image);
圖片,通常情況下,無論有什麼屬性,他最基本的有2點,<img開頭, 有src屬性!
那麼只要匹配到這2個特徵,其他的就別管他,這樣,所有圖片就出來了
㈣ HTML中img標簽的src填本地絕對路徑無法顯示
在php中,可以通過正則表達式來獲得img標簽的src內容,下派培面分享下php如何獲取html標簽img的src內頌橋容。
1、首先新建一個php文件,命名為test.php,在test.php文件中,將img圖片塵櫻唯標簽存在$html變數中。
㈤ php匹配<img/>,添加width,height
這個問題你想復雜了,其實直接在前台用CSS樣式控制就可以了。
比如你的通過編輯器編輯的內容最終在一個類樣式為.content的DIV中顯示,則添加樣式
.content img{width:100px;height:100px;border:0px;}
就可以控制這個DIV下所有的圖像了,沒必要去程序中處理。
或者通過JS控制也是可行的方法.
假設顯示這些圖片的DIV容器的ID是content
<div id="content"></div>
<script language="javascript">
function DrawImage(ImgD,w,h){
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
if(image.width/image.height>1){
if(image.width>w){
ImgD.width=w;
ImgD.height=(image.height*w)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
}
else{
if(image.height>h){
ImgD.height=h;
ImgD.width=(image.width*h)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
}
}
}
var images =document.getElementById("content").getElementsByTagName("img");
for(var i=0;i<images.length;i++){
var img =images[i];
DrawImage(img,100,100);
}
</script>