php正则匹配图片地址,代码如下:
<?php
//图片地址 正则匹配
//http://www.
function Getpics($body)
{
global $weburl;
//$body = strtolower($body);
$img_array = array();
preg_match_all("/(src|SRC)=[\"|'| ]{0,}((http|HTTP):\/\/(.*)\.(gif|jpg|GIF|JPG|png))/isU",$body,$img_array);
$img_array = array_unique($img_array[2]);
foreach($img_array as $key=>$value)
{
$url = $value;
$fileurl = "/pic.php?s=".base64_encode($url);
$body = str_replace($url,$fileurl,$body);
}
return $body;
}
?>
您可能感兴趣的文章:
PHP正则匹配日期和时间(时间戳转换)的例子
php匹配任何网址的正则表达式
php正则匹配重写html图片img路径的代码一例
PHP正则匹配获取URL中域名的代码
使用 preg_replace 函数 匹配图片并加上链接的方法
php用preg_match_all匹配文章中的图片
php正则表达式匹配URL中的域名
在php中可以使用内置函数gethostbyname获取域名对应的IP地址,比如:
<?php
echo gethostbyname("www.");
?>
以上会输出域名所对应的的IP。
对于做了负载与cdn的域名来讲,可能返回的结果会有不同,这点注意下。
下面来说说获取域名的方法,例如有一段网址:http://www./all-the-resources-of-this-blog.html
方法1,
echo $_SERVER[“HTTP_HOST”];
//则会输出www.
本地测试则会输出localhost。
方法2,使用parse_url函数;
<?php $url ="http://www./index.php?referer="; $arr=parse_url(/blog_article/$url/index.html); echo "<pre>"; print_r($arr); echo "</pre>"; ?>
输出为数组,结果为:
(
[scheme] => http
[host] => www.
[path] => /index.php
[query] => referer=
)
说明:
scheme对应着协议,host则对应着域名,path对应着执行文件的路径,query则对应着相关的参数;
方法3,采用自定义函数。
<?php
$url ="http://www./index.php?referer=";
get_host($url);
function get_host($url){
//首先替换掉http://
$url=Str_replace("http://","",$url);
//获得去掉http://url的/最先出现的位置
$position=strpos($url,"/");
//如果没有斜杠则表明url里面没有参数,直接返回url,
//否则截取字符串
if($position==false){
echo $url;
}else{
echo substr($url,0,$position);
}
}
?>
方法4,使用php正则表达式。
<?php
header("Content-type:text/html;charset=utf-8");
$url ="http://www./index.php?referer=";
$pattern="/(http:\/\/)?(.*)\//";
if(preg_match($pattern,$url,$arr)){
echo "匹配成功!";
echo "匹配结果:".$arr[2];
}
?>
您可能感兴趣的文章:PHP获取域名的几个全局变量
php 实现dns域名查询的方法详解(图文)
php 从url中获取域名的实例代码
php获取站点的来路域名的方法
php获取URL中domain域名的代码一例
PHP正则匹配获取URL中域名的代码
PHP获取当前网址及域名的代码
php正则表达式匹配URL中的域名
PHP调用万网接口实现域名查询的功能
实现:
下载网站的所有图片,即css代码中用到的图片,纯php代码实现。
代码如下:
<?php
/*
* 下载css样式文件中的图片
* edit by www.
*/
//设置PHP超时时间
set_time_limit(0);
//取得样式文件内容
$styleFileContent = file_get_contents(‘images/style.css’);
//匹配出需要下载的URL地址
preg_match_all(“/url\((.*)\)/”, $styleFileContent, $imagesURLArray);
//循环需要下载的地址,逐个下载
$imagesURLArray = array_unique($imagesURLArray[1]);
foreach($imagesURLArray as $imagesURL) {
file_put_contents(basename($imagesURL), file_get_contents($imagesURL));
}
?>
这种功能的实现,使用php curl函数最为方便,毕竟curl在采集方面是有专长的。
有关curl的内容,可以参考如下的文章:
您可能感兴趣的文章:
php curl上传文件的简单例子
解决php中不加载php_curl.dll扩展的问题
php curl实现get,post和cookie的实例代码
php中curl网络处理的应用实例
php中开启curl扩展的方法详解
php curl应用实例分析
php curl用法的实例代码
PHP中用CURL伪造IP来源的方法
php使用curl判断远程文件是否存在的代码
php模拟登录qq邮箱(curl命令详解)
php curl 学习总结