㈠ 用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>