原文链接:http://gouji.org/2014/05/254.html
由于所用虚拟主机 不支持 mail
函数,所以 访客评论邮件提示一直无法使用,百度 N 多代码后,终于自己摸索出来了。
网上大多代码 不能使用的原因是因为 From
参数 是获取的管理员 邮箱地址,
经本人测试 From
应该与 Username
保持一致。
具体实现代码为:
/**
* PHP 正则 采集 wiki 历史上的今天 (366 天)
**/
$arr_31 = array(1, 3, 5, 7, 8, 10, 12);
for($month=1; $month < = 12; $month++){
if($month == 2) $days = 29;
elseif(in_array($month, $arr_31)) $days = 31;
else $days = 30;
for($day=1; $day <= $days; $day++){
echo "$month-$day" . PHP_EOL;
$file = "./arr/$month-$day.txt";
$url = "http://zh.wikipedia.org/zh-cn/$month%E6%9C%88$day%E6%97%A5";
// 获取 保存数据
_put($file, _g($url));
}
}
// 保存数组为文件
function _put($file, $arr){
file_put_contents($file, var_export($arr, true));
}
// 远程获取 文件内容 正则 匹配出 大事件、出生、逝世 数据
function _g($url){
$str = file_get_contents($url);
if(!preg_match("/id=\".E5.A4.A7.E4.BA.8B.E8.A8.98\"(.*)<h2>/Usim", $str, $js)) preg_match("/id=\".E5.A4.A7.E4.BA.8B.E8.AE.B0\"(.*)<h2>/Usim", $str, $js);
$_js = _t($js[1]);
preg_match("/id=\".E5.87.BA.E7.94.9F\"(.*)</h2><h2>/Usim", $str, $cs);
$_cs = _t($cs[1]);
if(!preg_match("/id=\".E9.80.9D.E4.B8.96\"(.*)</h2><h2>/Usim", $str, $ss)) preg_match("/id=\".E5.8E.BB.E4.B8.96\"(.*)</h2><h2>/Usim", $str, $ss);;
$_ss = _t($ss[1]);
return array($_js, $_cs, $_ss);
}
// 正则 匹配 合适的结果
function _t($str){
preg_match_all("/<li>(.*)< \/li>/Usim", $str, $arr);
$Arr = array();
foreach($arr[1] as $K => $V){
$dat = strip_tags($V);
if(preg_match('/年[:|:]/', $dat)){
$Arr[] = $dat;
}
}
return $Arr;
}
别问我怎么用
问了我也不会告诉你 以上代码放在 主题 functions.php
文件就能生效的
以后就可以使用 wp_mail
函数发送邮件了 wp_mail("[email protected]", "邮件主题", "邮件正文");