有人回复时邮件通知我(WordPress) SAE 移植版

常见的 Wordress 邮件通知功能,使用起来非常的简单

1,先配置一下您的SMTP账户的用户名和密码。
2,将下面的内容粘贴到当前主题的 functions.php 中即可,注意别破坏了原来的结构就行。(或者直接建一个文件,将代码拷贝进去,然后在 functions.php 中引用该文件即可。)

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
 
<?php
/**
 * Email Me With The Replay For SAE
 * 
 * Usage:将此段代码粘贴到当前主题的 functions.php 文件中即可。
 * 或者将此文件放进主题目录,在 functions.php 中包含此文件
 * 
 * 基于 comment_mail_notify v1.0 by willin kan. http://kan.willin.org/?p=1295(已失效)
 * changelist
 * 2012-03-29
 * version 0.1
 * 
 * 2012-04-29
 * version 0.2
 * edit $subject to this code
 * $subject = '您的评论 ' . mb_substr(strip_tags(get_comment($parent_id)->comment_content), 0, 10, 'utf-8') . ' 有了回应 - ' . get_option("blogname");
 * 
 * 
 * @author IT不倒翁, willin kan
 * @copyright (C) 2011,2012 Just Use It!
 * @link yungbo.com
 * @version 0.2
 */
function comment_mail_notify($comment_id) {
	//换行标志
	$lineBreak = "\n";
	//缩进标志
	$tabBreak = "\t";
 
	$admin_notify         = '1'; // are you willing to receive the email? 1 is yes.
	$admin_email          = get_bloginfo('admin_email'); // you can change $admin_email to your e-mail optionaly.
	$comment              = get_comment($comment_id);
	$comment_author_email = trim($comment->comment_author_email);
	$parent_id            = $comment->comment_parent ? $comment->comment_parent : '';
	global $wpdb;
	if ($wpdb->query("Describe {$wpdb->comments} comment_mail_notify") == '')
	$wpdb->query("ALTER TABLE {$wpdb->comments} ADD COLUMN comment_mail_notify TINYINT NOT NULL DEFAULT 0;");
	if (($comment_author_email != $admin_email && isset($_POST['comment_mail_notify'])) || ($comment_author_email == $admin_email && $admin_notify == '1'))
	$wpdb->query("UPDATE {$wpdb->comments} SET comment_mail_notify='1' WHERE comment_ID='$comment_id'");
	$notify         = $parent_id ? get_comment($parent_id)->comment_mail_notify : '0';
	$spam_confirmed = $comment->comment_approved;
	if ($parent_id != '' && $spam_confirmed != 'spam' && $notify == '1') {
		$wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])); // e-mail
		$to       = trim(get_comment($parent_id)->comment_author_email);
 
		$subject = '您的评论 ' . mb_substr(strip_tags(get_comment($parent_id)->comment_content), 0, 10, 'utf-8') . ' 有了回应 - ' . get_option("blogname");
 
		//邮件主体内容
          	//估计新浪考虑到安全问题,全部用 htmlspecialchars() 过滤了,所以不要使用 html 标记
		$message = '您好,' . trim(get_comment($parent_id)->comment_author) . $lineBreak;
		$message .= '您曾在 ' . get_option("blogname") . ' 留言:' . $lineBreak;
		$message .= $tabBreak . trim(get_comment($parent_id)->comment_content) . $lineBreak;
		$message .= trim($comment->comment_author) . ' 回复了您:' . $lineBreak;
		$message .= $tabBreak . trim($comment->comment_content) . $lineBreak;
		$message .= '您可以点击 ' . get_comment_link($parent_id) . ' 查看具体情况' . $lineBreak;
 
		$message .= $lineBreak;
		$message .= '(系统邮件,请不要直接回复)' . $lineBreak;
		$message .= get_option('blogname') . $lineBreak;
		$message .= home_url() . $lineBreak;
 
		/*SAE 配置位置*/
		$saeTo = $to;
		$saeSubject = $subject;
		$saeMessage = $message;
		$saeSMTPUser = '';//SMTP邮箱
		$saeSMTPPass = '';//SMTP密码
 
		$mail = new SaeMail();
		$result = $mail->quickSend($saeTo, $saeSubject, $saeMessage, $saeSMTPUser, $saeSMTPPass);
//		if ($result === false)
//			var_dump($mail->errno(), $mail->errmsg());
 
	}
}
add_action('comment_post', 'comment_mail_notify');
 
/* Auto checked */
function add_checkbox() {
	echo '<input type="checkbox" name="comment_mail_notify" id="comment_mail_notify" value="comment_mail_notify" checked="checked" /><label for="comment_mail_notify">有人回复我时邮件通知我</label>';
}
 
add_action('comment_form', 'add_checkbox');

代码最近更新日期,2012-04-29

代码地址 http://itbdw.sinaapp.com/util/Email_Me_With_Reply_SAE.txt

正则表达式常用元字符

以下关于正则表达式的内容来自 MSDN 和维基百科,仅供自己查阅方便。

表达式全集

正则表达式有多种不同的风格。下表是在PCRE中元字符及其在正则表达式上下文中的行为的一个完整列表:

下表包含了元字符的完整列表以及它们在正则表达式上下文中的行为:

谷歌手机地图 百度手机地图对比(安卓客户端)

测试日期:2012年2月19日
测试版本:均为当前最新版本
测试环境:晴天,室外,森林

测试总结:
1,百度手机地图(安卓)路线准确,沥青路在地图上均有显示,不过仅此而已,路边绿化在地图上没有特殊标示。
2,谷歌地图(安卓)路线不太准确,信息滞后,拆迁的地方没有更新,按照地图上走过去,发现已被夷为平地。。。信息全面,对树木、河流均有标示,定位精确度比百度稍高。

综上,建议两个都安装。平时用百度,需要查看更多信息时可参考谷歌地图,若需要更多信息,可打开它们的卫星地图图层。

PHP 将逗号、空格、回车分隔的字符串转换为数组

我们在搜索一些东西时会经常遇到可以通过空格隔开来达到输入多个条件的目的。今天正好项目中遇到了这个情况,就写了一个函数,将多个条件放到数组里。目前支持空格、逗号(中英文)、回车分割,如不能满足需求,看下这个函数修改一下应该就可以了。

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
 
<?php
/**
* transform ' hello, world !' to array('hello', 'world')
*/
function strsToArray($strs) {
$result = array();
$array = array();
 
$strs = str_replace(',', ',', $strs);
$strs = str_replace("n", ',', $strs);
$strs = str_replace("rn", ',', $strs);
$strs = str_replace(' ', ',', $strs);
 
$array = explode(',', $strs);
foreach ($array as $key => $value) {
if ('' != ($value = trim($value))) {
$result[] = $value;
}
}
return $result;
}
 
//test
$strs = 'Code is poetry! WTF!';
var_dump(strsToArray($strs));

PHP 判断字符串是否包含其它字符

以下几个函数均可用来判断某字符串是否包含另外一个字符串PHP 中判断一个字符串是否包含其它字符是很常见的操作。 虽然很简单,但还是写了几个函数,质量可能不是很高,权当锻炼。 如果这几个函数恰好能帮上你的忙,我将会很高兴的。这几个函数中,我比较喜欢第四个。。。

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
/**
 * 以下几个函数均可用来判断某字符串是否包含另外一个字符串
 * PHP 中判断一个字符串是否包含其它字符是很常见的操作。
 * 虽然很简单,但还是写了几个函数,质量可能不是很高,权当锻炼。
 * 如果这几个函数恰好能帮上你的忙,我将会很高兴的。
 */
 
/**
 * 利用一下 strpos() 函数
 * @param unknown_type $haystack
 * @param unknown_type $needle
 */
function isInString1($haystack, $needle) {
 //防止$needle 位于开始的位置
 $haystack = '-_-!' . $haystack;
 return (bool)strpos($haystack, $needle);
}
 
/**
 * 利用字符串分割
 * @param unknown_type $haystack
 * @param unknown_type $needle
 */
function isInString2($haystack, $needle) {
 $array = explode($needle, $haystack);
 return count($array) &gt; 1;
}
 
/**
 * 用了一下正则,这种方法十分不建议,尤其是 $needle 中包含
 * 特殊字符,如 ^,$,/ 等等
 * @param unknown_type $haystack
 * @param unknown_type $needle
 */
function isInString3($haystack, $needle) {
 $pattern = '/' . $needle . '/';
 return (bool)preg_match($pattern, $haystack);
}
 
/**
 * 利用一下 strpos() 函数
 * @param unknown_type $haystack
 * @param unknown_type $needle
 */
function isInString4($haystack, $needle) {
 return false !== strpos($haystack, $needle);
}
 
//测试
$haystack = 'I am ITBDW';
$needle = 'IT';
var_dump(isInString1($haystack, $needle));

我觉得最简单的就是这种了 strpos($a, $b) !== false 如果$a 中存在 $b,则为 true ,否则为 false。

用 !== false (或者 === false) 的原因是如果 $b 正好位于$a的开始部分,那么该函数会返回int(0),那么0是false,但$b确实位于$a中,所以要用 !== 判断一下类型,要确保是严格的 false。昨天晚上去中关村图书大厦,看到一本书中用的是 strpos === true 来判断,这是极其不正确的。。。

出错的书为《PHP求职宝典》107页(2012-02-26更新)

其它的还有 PHP 原生支持的函数,如 strstr(),stristr() 等,直接判断就可以了。

JSON 快速入门

JSON,即 Javascript Object Notation。

JSON 格式的数据包括 json 对象和 json 数组:

  1. json 对象:由一对大括号包裹,一对或多对键值对组成的无序集合。其中键值对用冒号分隔,对各键值对之间用逗号分隔。如 {“name” : “Zhao”,  ”age” : “30″}
  2. json 数组:由中括号包裹,由逗号分隔的有序集合。其值范围很广(具体请参考 json 官网并加以测试),如 ["zhao", 21, true, false, null, {"name":"Zhao"}, ["hello", "world"]]

对象

数组


JSON 数据在 PHP 中的应用

掌握两个函数 json_encode() 和 json_decode()

json_encode() 将数组或者对象转换为 json 格式

json_decode() 如果第二个参数为 true,则将 json 对象转换为数组,包括 json 数组中的对象。

知识扩展:
json官网:http://json.org/json-zh.html
JSON维基百科:http://zh.wikipedia.org/wiki/JSON
JSON@PHP.NET:http://php.net/manual/zh/book.json.php