<?php $strTimeToString = "000111222334455556666667"; $strWenhou = array('夜深了,','凌晨了,','早上好!','上午好!','中午好!','下午好!','晚上好!','夜深了,'); echo $strWenhou[(int)$strTimeToString[(int)date('G',time())]]; ?>
无折腾,不生活。
<?php $strTimeToString = "000111222334455556666667"; $strWenhou = array('夜深了,','凌晨了,','早上好!','上午好!','中午好!','下午好!','晚上好!','夜深了,'); echo $strWenhou[(int)$strTimeToString[(int)date('G',time())]]; ?>
请教个问题.以下是一个PHP文件全部代码
********************************
<?php
function RandomFile($folder='', $extensions='.*'){
// fix path:
$folder = trim($folder);
$folder = ($folder == '') ? './' : $folder;
// check folder:
if (!is_dir($folder)){ die('invalid folder given!'); }
// create files array
$files = array();
// open directory
if ($dir = @opendir($folder)){
// go trough all files:
while($file = readdir($dir)){
if (!preg_match('/^\.+$/', $file) and
preg_match('/\.('.$extensions.')$/', $file)){
// feed the array:
$files[] = $file;
}
}
// close directory
closedir($dir);
}
else {
die('Could not open the folder "'.$folder.'"');
}
if (count($files) == 0){
die('No files where found :-(');
}
// seed random function:
mt_srand((double)microtime()*1000000);
// get an random index:
$rand = mt_rand(0, count($files)-1);
// check again:
if (!isset($files[$rand])){
die('Array index was not found! very strange!');
}
// return the random file:
return $folder . $files[$rand];
}
//用法演示:
// "jpg|png|gif" matches all files with these extensions
//print RandomFile('test_images/','jpg|png|gif');
// returns test_07.gif
// ".*" matches all extensions (all files)
//print RandomFile('test_files/','.*');
// returns foobar_1.zip
// "[0-9]+" matches all extensions that just
// contain numbers (like backup.1, backup.2)
//print RandomFile('test_files/','[0-9]+');
// returns backup.7
//随机从file.txt文本文件中读取一行出来
$file = RandomFile('so/data/sitemap/','.*'); //文件位置;
//是否存在
if(file_exists($file)){
$data = file($file); //将文件存放在一个数组中;
$num = count($data); //条数;
$id = mt_rand(0,$num-1); //随机数字;
$cn_text = chop($data[$id]); //显示第几行数据,并去除空格;
}
function characet($cn_text){
if( !empty($cn_text) ){
$fileType = mb_detect_encoding($cn_text , array('UTF-8','GBK','LATIN1','BIG5')) ;
if( $fileType != 'UTF-8'){
$data = mb_convert_encoding($cn_text ,'utf-8' , $fileType);
}
}
return $cn_text;
}
//header ('Content-Type:text/html;charset=utf-8');
//echo ($cn_text);
//echo 'document.write(“‘. $cn_text .'”);’;
//echo “document.getElementById(‘”.$keywords.”‘).innerHTML='”.$cn_text.”‘;”;
*************************************
请问在html文件中如何调用这个文件随机从txt当中读取的字符串?
方式很多,常见的是iframe
非常好,不错,吸纳了。
Usually I do not read post on blogs, however I would like to say that this write-up very forced me to take a look at and do so! Your writing style has been surprised me. Thanks, very great post.