下面我来给大家推荐一个网友写的php递归遍历之遍历文件夹下的所有文件和子文件实例代码,希望对各位朋友有所帮助。
写一个函数,能够遍历一个文件夹下的所有文件和子文件夹。
代码如下 |
复制代码 |
$dirs='e:/pdf';
function FileShow($dirs) {
$dir=opendir($dirs);
while ($f=readdir($dir)) {
if($f != '.' && $f != '..'){
$file=$dirs.'/'.$f;
if(is_file($file)){
echo 'FileName:'.$file.'<br />';
//echo 'FileName:'.iconv('gb2312','utf-8',$file).'<br />';
}else{
FileShow($file);
}
}
}
}
FileShow($dirs);
|
在php中生成随机数据我们可以使用rand,mt_rand都可以生成指定范围内随机数据了,下面小编来给各位同学介绍一下方法。
调用mt_rand()这个方法可以生成随机数字,参数是范围的最小值和最大值,函数会返回最小值和最大值之间的一个随机数字。
要生成真正的随机数,对于计算来说不是一件容易的事。
php中两种方法可以生成随机数,一个经典的函数叫rand(),另一个更出色的函数是mt_rand()。
例1
代码如下 |
复制代码 |
$random =rand(0,1000);
或者
<?php
$rand = mt_rand(1, 100);
echo $rand;
?>
|
例2
代码如下 |
复制代码 |
srand((double)microtime()*1000000);
$random =rand(0,1000);
|
例3
代码如下 |
复制代码 |
/**
*获取一定范围内的多个随机数字
*/
function yang_numberRand($begin = 0, $end = 20, $limit = 5){
$rand_array = range($begin, $end);
shuffle($rand_array); //调用现成的数组随机排列函数
return array_slice($rand_array, 0, $limit); //截取前$limit个
}
|
在php中301重定向实现方法很简单我们只要简单的利用header发送301状态代码,然后再用header进行跳转,效果与apache,iis,nginx都是一样的效果哦。
一:更推荐这种方法,因为它可以把www.111cn.net原来所有的url都转到111cn.net新的地址上
代码如下 |
复制代码 |
<?php
$the_host = $_SERVER['HTTP_HOST'];
$request_uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
if($the_host == 'www.111cn.net')
{
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://111cn.net'.$request_uri);//
}
?>
|
二:单页多站的Php301重定向代码,www.111cn.net和111cn.net则301到index.php上,www.111cn.net则301到111cn.net上,否则转到错误页
代码如下 |
复制代码 |
if(($HTTP_HOST=="www.111cn.net")or($HTTP_HOST=="111cn.net"))
{
header("HTTP/1.1 301 Moved Permanently");
Header("Location: /index.php");
}
elseif($HTTP_HOST=="www.111cn.net")
{
header("HTTP/1.1 301 Moved Permanently");
Header("Location: http://111cn.net");
}
else
{
Header("Location: /404.htm");
}
?>
|
附上其它跳转办法
代码如下 |
复制代码 |
//定义编码
header( 'Content-Type:text/html;charset=utf-8 ');
//Atom
header('Content-type: application/atom+xml');
//CSS
header('Content-type: text/css');
//Javascript
header('Content-type: text/javascript');
//JPEG Image
header('Content-type: image/jpeg');
//JSON
header('Content-type: application/json');
//PDF
header('Content-type: application/pdf');
//RSS
header('Content-Type: application/rss+xml; charset=ISO-8859-1');
//Text (Plain)
header('Content-type: text/plain');
//XML
header('Content-type: text/xml');
// ok
header('HTTP/1.1 200 OK');
//设置一个404头:
header('HTTP/1.1 404 Not Found');
//设置地址被永久的重定向
header('HTTP/1.1 301 Moved Permanently');
//转到一个新地址
header('Location: http://www.example.org/');
//文件延迟转向:
header('Refresh: 10; url=http://www.example.org/');
print 'You will be redirected in 10 seconds';
//当然,也可以使用html语法实现
// <meta http-equiv="refresh" content="10;http://www.example.org/ />
// override X-Powered-By: PHP:
header('X-Powered-By: PHP/4.4.0');
header('X-Powered-By: Brain/0.6b');
//文档语言
header('Content-language: en');
//告诉浏览器最后一次修改时间
$time = time() - 60; // or filemtime($fn), etc
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT');
//告诉浏览器文档内容没有发生改变
header('HTTP/1.1 304 Not Modified');
//设置内容长度
header('Content-Length: 1234');
//设置为一个下载类型
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="example.zip"');
header('Content-Transfer-Encoding: binary');
// load the file to send:
readfile('example.zip');
// 对当前文档禁用缓存
header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Pragma: no-cache');
//设置内容类型:
header('Content-Type: text/html; charset=iso-8859-1');
header('Content-Type: text/html; charset=utf-8');
header('Content-Type: text/plain'); //纯文本格式
header('Content-Type: image/jpeg'); //JPG***
header('Content-Type: application/zip'); // ZIP文件
header('Content-Type: application/pdf'); // PDF文件
header('Content-Type: audio/mpeg'); // 音频文件
header('Content-Type: application/x-shockw**e-flash'); //Flash动画
//显示登陆对话框
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Basic realm="Top Secret"');
print 'Text that will be displayed if the user hits cancel or ';
print 'enters wrong login data';
|
跳转要注意以下几点,有助于解决一些新手经常遇到的问题
1、location和“:”号间不能有空格,否则会出错。
2、在用header前不能有任何的输出。
3、header后的PHP代码还会被执行。
在php中数组写入文件的方法有很多种,像我们要做成缓存文件就会把php数组转换成文件然后保存到.php文件,调用时直接调用此文件即可。
php把数组写入文件,通过序列和反序列实现。请看代码
$file="./data/file.cache";
file_put_contents($file,serialize($array));//写入缓存
代码如下 |
复制代码 |
<?php
$file="./data/file.cache";
$array = array("count" => "3000",
"num" =>"300");
//缓存
file_put_contents($file,serialize($array));//写入缓存
?>
<?
$file="./data/file.cache";
$handle = fopen($file, "r");
$cacheArray = unserialize(fread($handle, filesize ($file)));
print_r($cacheArray);
?>
|
//将一个测试的数组写入一个PHP文件:
代码如下 |
复制代码 |
<?php //要写入PHP文件的数组
$write_array = array( '1' => 'oneone', '2' => 'two', '3' => 'three', '4' => 'four', '5' => 'five' ); //字符串处理
$string_start = "<?phpn"; $string_process = var_export($write_array, TRUE); $string_end = "n?>"; $string = $string_start.$string_process.$string_end; //开始写入文件
echo file_put_contents('test_array.php', $string);
?>
|
这里用到了两个函数:
代码如下 |
复制代码 |
1,var_export():
|
·var_export — 用来输出或返回一个变量的字符串表示,它和 var_dump() 的区别是,var_export() 可以用来返回关于传递给该函数的变量的结构信息,并且其返回的表示是合法的 PHP 代码如果 “echo $string_process;”,则可以看到输出结果:
代码如下 |
复制代码 |
array ( 1 => 'oneone', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five', )
|
而它就是我们要写入 test_array.php 文件的内容(除去 php 标签);
·var_dump() 函数用来打印变量的相关信息,它只用来“打印”,而不会返回值,它的原型是 void var_dump(……),我们来 “var_dump($string_process);”,则可以看到输出结果:
代码如下 |
复制代码 |
string(86) "array ( 1 => 'oneone', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five', )"
|
可以看到输出的string(86) “…”,再一次说明了 var_export() 返回的是一个字符串。
在做内容站时,经常会用到采集软件在互联网上大肆侵略别站的资源,一采集就是几千篇的文章,采集之后 发现内容中有些原站点的超级链接,要是一个个去改的话 很麻烦 所以写了个方法,测试成功。
简单说一下原理,这里重利用的是 PHP 的替换函数 preg_replace,在实际应用中,我们经常使用 preg_replace 去替换一些危险字符或去转换一些斜杠或回车等。preg_replace($1,$2,$3) 有三个重要的参数,其中 $1 是要搜索的字符串,$2 是要替换成的字符串,$3 是要进行替换的字符串。
那么知道了 preg_replace 函数工作的原理,那么进行替换超链接就不难了,我们只需要将参数 $1 和 $2 转换成数组,进行批量替换,以下是方法,测试成功,共享给 phper 。
代码如下 |
复制代码 |
<?php
$str="<a href="xxx">超级链接</a>|<a href="xxx">这是个链接</a><br>";
function removelink($str){
$mode=array("#<a href="(.*)">#iUs","#</a>#iUs");
$want=array("","");
$con=preg_replace($mode,$want,$str);
return $con;
}
echo removelink($str);
?>
|
或
代码如下 |
复制代码 |
<?php
$content = file_get_contents('test.html');
$url = 'http://www.111cn.net'; //要换成的新网址
$preg = '/[s]href=("|')[S]*("|')/i';
$replace = ' href="' . $url . '"';
$content = preg_replace($preg, $replace, $content); //正则替换
create_log('newhtml', $content); //生成新文件
?>
|
都是可以的
标签:[!--infotagslink--]