存档

文章标签 ‘PHP’

用php计算N以内的质数(素数)

2010年8月15日 yanglu 1 条评论

PHP常见的一道简单面试题,解答如下,欢迎交流:

function zhishu($n) 
{
    $arr = array();
    $n = intval($n);
    if(!$n || $n < 1) return;
    for($i=1;$i<=$n;$i++) 
    {
        for($j=2;$j<$i;$j++) 
        {
            if($i%$j == 0) continue 2;
        }
        $arr[] = $i;
    }
 
    return $arr;
}
print_r(zhishu(1000));
分类: PHP 标签:

ssh使用命令nohup php-cgi后台运行PHP

2010年6月10日 yanglu 5 条评论

使用ssh远程登录服务器的时候,要进行后台任务操作(比如采集),可以用命令nohup php-cgi来运行php,这样就可以关掉ssh客户端后还会在服务器上运行,运行的输出结果记录在用户目录下的nohup.out文件。

分类: Debian, PHP 标签: ,

SMARTY模板中如何使用get,post,request,cookies,session,server变量

2010年4月26日 yanglu 2 条评论

{$smarty}保留变量不需要从PHP脚本中分配,是可以在模板中直接访问的数组类型变量,通常被用于访问一些特殊的模板变量。例如,直接在模板中访问页面请求变量、获取访问模板时的时间邮戳、直接访问PHP中的常量、从配置文件中读取变量等。该保留变量中的部分访问介绍如下。

1.在模板中访问页面请求变量

我们可以在PHP脚本中,通过超级全局数组$_GET、$_POST、$_REQUEST获取在客户端以不同方法提交给服务器的数据,也可以通过$_COOKIE或$_SESSION在多个脚本之间跟踪变量,或是通过$_ENV和$_SERVER获取系统环境变量。如果在模板中需要这些数组,可以调用Smarty对象中的assign()方法分配给模板。但在Smarty模板中,直接就可以通过{$smarty}保留变量访问这些页面请求变量。在模板中使用的示例如下所示: 阅读全文…

分类: PHP 标签: ,

php正则匹配中文

2009年12月10日 yanglu 2 条评论

php正则匹配中文的方法:

$str = "04aol汉字";
$pattern = "/^(\d{2})([A-Za-z]{3})([".chr(0xa1)."-".chr(0xff)."]+)$/";
if(preg_match($pattern, $str, $tmp))
{
  var_export($tmp);
}

显示结果:

array (
  0 => '04aol汉字',
  1 => '04',
  2 => 'aol',
  3 => '汉字',
)
分类: PHP 标签: ,

ignore_user_abort实现计划任务

2009年11月24日 yanglu 6 条评论

ignore_user_abort,这个函数可以帮助我们实现像linux中的cron一样实现计划任务,用户关掉浏览器后还可以执行。
使用方法:先使用函数set_time_limit(0)设置程序的执行时间为无限制。
例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
// Ignore user aborts and allow the script
// to run forever
ignore_user_abort(true);
set_time_limit(0);
 
echo 'Testing connection handling in PHP';
 
// Run a pointless loop that sometime 
// hopefully will make us click away from 
// page or click the "Stop" button.
while(1)
{
    // Did the connection fail?
    if(connection_status() != CONNECTION_NORMAL)
    {
        break;
    }
 
    // Sleep for 10 seconds
    sleep(10);
}
 
// If this is reached, then the 'break' 
// was triggered from inside the while loop
 
// So here we can log, or perform any other tasks
// we need without actually being dependent on the 
// browser.
?>
分类: PHP 标签:

php配置变量写入配置文件的方法

2009年10月30日 yanglu 3 条评论

有些常用的配置变量写入MYSQL比较麻烦,可以写入一个php配置文件,在需要的地方引入这个配置文件即可。写入方法如下(摘入):

$configfile = @file_get_contents('./config.inc.php');
			$configfile = trim($configfile);
			$configfile = preg_replace("/[$]dbhost\s*\=\s*[\"'].*?[\"'];/is", "\$dbhost = '$dbhost';", $configfile);
			$configfile = preg_replace("/[$]dbuser\s*\=\s*[\"'].*?[\"'];/is", "\$dbuser = '$dbuser';", $configfile);
			$configfile = preg_replace("/[$]dbpw\s*\=\s*[\"'].*?[\"'];/is", "\$dbpw = '$dbpw';", $configfile);
			$configfile = preg_replace("/[$]dbname\s*\=\s*[\"'].*?[\"'];/is", "\$dbname = '$dbname';", $configfile);
			$configfile = preg_replace("/[$]adminemail\s*\=\s*[\"'].*?[\"'];/is", "\$adminemail = '$adminemail';", $configfile);
			$configfile = preg_replace("/[$]tablepre\s*\=\s*[\"'].*?[\"'];/is", "\$tablepre = '$tablepre';", $configfile);
			$configfile = preg_replace("/[$]cookiepre\s*\=\s*[\"'].*?[\"'];/is", "\$cookiepre = '".random(3)."_';", $configfile);
 
			@file_put_contents('./config.inc.php', $configfile);
分类: Discuz!, PHP 标签: ,

PHP文件下载 浏览器显示文件名乱码问题

2009年9月29日 yanglu 1 条评论
<?php 
$filename = "下载文件名";
$filesize = filesize('文件地址'); //获得文件大小 
header('Pragma: public'); 
header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT'); 
header('Cache-Control: no-store, no-cache, must-revalidate'); 
header('Cache-Control: pre-check=0, post-check=0, max-age=0'); 
header('Content-Transfer-Encoding: binary');
header('Content-Encoding: none'); 
header('Content-type: application/force-download'); 
header('Content-Disposition: attachment; filename="'.$filename.'"'); 
header('Content-length: '.$filesize);
?>

这样在弹出文件下载框时,显示的文件名在不同的浏览器下面是不一样的。有的是乱码,有的是空白。
所以文件名统一使用utf-8编码,然后针对ie浏览器进行一次rawurlencode编码。
把以下代码放在header之前

if (preg_match(/MSIE/,$_SERVER['HTTP_USER_AGENT'])) {
        $filename = rawurlencode($filename);
}
分类: PHP, 网页制作 标签: ,