php array_filter对数组进行过滤

将一个数组中能被 3 整除的数保留下来,需要写回调函数,满足条件的返回 true,否则返回 false

    $arr = array(1,261,262,263,264,265,266,267,268,269,270,271,272,273,275,274,276,279,277,280,278,281,282,287,283,288,289);
    function myFun($var){
        if($var%3==0)
            return true;
        else
            return false;
    }

    $filtered = array_filter($arr,"myFun");
    print_r($filtered); 

上面针对的是 value, 可以只针对 key 或针对 key, value 进行过滤

http://php.net/manual/en/function.array-filter.php

email的html排版

用程序生成好一个邮件标题和内容,之后想要完美的在收件箱里接收,需要编写针对于邮件的html。

美观可以放在第二位,但是需要让邮箱认出是html来,否则内容会以原码显示。

在邮件headers中加入正确的Content-type和charset即可。

email html中的布局基本是用table,css样式尽量直接写在html标签里,以下分别是用原生php和php-mail(SMTP)来发邮件。

<?php

$message='

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>title</title>
</head>
<body>
<table>
<tr>
<th>three days forcast</th>
</tr>
</table>
</body>
</html>';

$to = 'youmail@gmail.com';

$subject =send mail using php;
$from = "yourmail@163.com";
$headers = "From: $from\n";
$headers .= "Reply-To: $from\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1";

mail($to, $subject, $message,$headers);
echo "Mail Sent.";

?>

使用pear mail(SMTP)发送邮件

<?php

//require apt-get install php-mail, reference: http://php.net/manual/en/function.mail.php
include("Mail.php");

$message='

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>title</title>
</head>
<body>
<table>
<tr>
<th>three days forcast</th>
</tr>
</table>
</body>
</html>';

$to = 'youmail@gmail.com';

$subject =send mail using php;

$headers["From"] = "yourmail@163.com";
$headers["TO"] = "yourmail@gmail.com";
$headers["Subject"] = "$subject";
$headers["MIME-Version"] = "1.0";
$headers["Content-type"] = "text/html";
$headers["charset"] = "iso-8859-1";

$params["host"] = "smtp.163.com";
$params["port"] = "25";
$params["auth"] = true;
$params["username"] = "yourmail@163.com";
$params["password"] = "yourpassword";

$mail_object = & Mail::factory("smtp", $params);

$mail_object->send($to, $headers, $message);

//echo $message;
echo "Mail Sent.";

?>

参考:http://johndoesdesign.com/blog/2012/php/getting-your-php-html-email-to-render-in-gmail/

http://email.about.com/od/emailprogrammingtips/qt/PHP_Email_SMTP_Authentication.htm

在ubuntu下安装phpredis

翻译自:http://ricochen.wordpress.com/2012/03/25/install-phpredis-on-ubuntu/ 打不开不怪我。

在12.04 TLS下测试可用。

1) 准备工作

 sudo apt-get install php5-dev

php5-dev 提供了开发库及编译必须的phpize工具

2) 获取phpredis源代码

git clone git://github.com/nicolasff/phpredis.git

3) 编译安装

cd phpredis
phpize5
./configure
make
sudo -s make install

4) 启用phpredis扩展

sudo -s
echo "extension=redis.so">/etc/php5/conf.d/redis.ini
exit

5) 写一段php代码进行测试 (在命令行下测试需要先安装好php5-cli)。以下是分别使用phpredis与Rediska(https://github.com/Shumkov/Rediska)进行测试。很明显使用c编写的phpredis插件性能高于php库Rediska。

phpredis_set.php内容和测试结果:

<?php
 // phpredis_set.php
 $redis=new Redis() or die("Can'f load redis module.");
 $redis->connect('127.0.0.1');
 $redis->set('set_testkey', 1);

time for i in `seq 1 1000`; do php phpredis_set.php; done

real 0m16.352s
user 0m8.105s
sys 0m5.784s

rediska_set.php内容和测试结果:

<?php
 require_once 'Rediska.php';
 $rediska=new Rediska();
 $rediska->set('set_testkey', 1);

time for i in `seq 1 1000`; do php rediska_set.php; done

real 0m21.075s
user 0m12.657s
sys 0m6.228s