小偷程序:把远程网站上的数据(图片,网页及其他文件)抓取到本地,处理后再显示
正则表达式:用于字符串的模式分割 、匹配、查找及替换操作。
相关函数:
int ereg ( string $pattern , string $string [, array &$regs ]
)
若省略参数返回的数组,找到则返回值为 True 否则 返回 False
与之对应 eregi() 不区分大小写。
string file_get_contents ( string $filename [, bool $use_include_path =
false [, resource $context[, int $offset =
0 [, int $maxlen ]]]]
)
读取整个文件,比如:
立即学习“PHP免费学习笔记(深入)”;
用此函数可以获取网页信息
他就是小偷程序的基础。
比如:
$url=file_get_contents("http://www.ubuntu.org.cn/index_kylin");
echo $url;
?>
但是对于另一个网站:
$url=file_get_contents("http://www.alangzhong.com/index.html");
echo $url;
?>
发现很多的背景图片是看不见的。

查看网页源代码我们发现,这是
src="/upload/201503/b123ec26-bb8f-43be-b5ad-cdf45153d053.png"/>
图片的地址使用了相对路径,而我们本地没有这样的文件,当然显示不出来。
用正则表达式选定图片,然后远程地址替换相对路径:
下面代码的超时问题没有解决。
<?php
//ini_set('max_execution_time', '0'); //三者都没用啊,一直超时
//@ini_set('default_socket_timeout', 20000);
//set_time_limit(2);
$url=file_get_contents("http://www.alangzhong.com/index.html");
//echo $url;
$fp = @fopen($url, "r") or die("超时"); //为什么不断超时
$contents = file_get_contents($url);
eregi("<img style="max-width:90%"116\" height=\"98\" src=\"/upload/201503/b123ec26-bb8f-43be-b5ad-cdf45153d053.png\"/ alt="PHP 简单的小偷程序" >",$contents,$rg);
// 远程地址替换相对路径
$rg[1]=str_replace("src=\"../upload/","src=\"http://www.alangzhong.com/index.html/upload/",$rg[1]);
echo $rg[1];
?>以上就介绍了PHP 简单的小偷程序,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号