存档

文章标签 ‘PHP’

PHP 中文字符串截取方法汇总

2011年3月7日 admin 1 条评论

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
/**
 * @todo 中文截取,支持gb2312,gbk,utf-8,big5
 *
 * @param string $str 要截取的字串
 * @param int $start 截取起始位置
 * @param int $length 截取长度
 * @param string $charset utf-8|gb2312|gbk|big5 编码
 * @param $suffix 是否加尾缀
 */
function CsubStrPro($str, $start = 0, $length, $charset = "utf-8", $suffix = true)
{
    if (function_exists ( "mb_substr" ))
        return mb_substr ( $str, $start, $length, $charset );
 
    $re ['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
    $re ['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
    $re ['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
    $re ['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
    preg_match_all ( $re [$charset], $str, $match );
    $slice = join ( "", array_slice ( $match [0], $start, $length ) );
    if ($suffix)
        return $slice . "…";
 
    return $slice;
}

2:自动识别 GBK 和UTF-8 编码的字符串

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
function cutStr($sourcestr, $cutstart=0, $cutlength) {
    $returnstr = '';
    $i = 0;
    $n = 0;
    $str_length = strlen ( $sourcestr ); //字符串的字节数
    while ( ($n < $cutlength) and ($i <= $str_length) ) {
        $temp_str = substr ( $sourcestr, $i, 1 );
        $ascnum = Ord ( $temp_str ); //得到字符串中第$i位字符的ascii码
        if ($ascnum >= 224) //如果ASCII位高与224,
        {
            $returnstr = $returnstr . substr ( $sourcestr, $i, 3 ); //根据UTF-8编码规范,将3个连续的字符计为单个字符
            $i = $i + 3; //实际Byte计为3
            $n ++; //字串长度计1
        }
        elseif ($ascnum >= 192) //如果ASCII位高与192,
        {
            $returnstr = $returnstr . substr ( $sourcestr, $i, 2 ); //根据UTF-8编码规范,将2个连续的字符计为单个字符
            $i = $i + 2; //实际Byte计为2
            $n ++; //字串长度计1
        }
        elseif ($ascnum >= 65 && $ascnum <= 90) //如果是大写字母,
        {
            $returnstr = $returnstr . substr ( $sourcestr, $i, 1 );
            $i = $i + 1; //实际的Byte数仍计1个
            $n ++; //但考虑整体美观,大写字母计成一个高位字符
        }
        else //其他情况下,包括小写字母和半角标点符号,
        {
            $returnstr = $returnstr . substr ( $sourcestr, $i, 1 );
            $i = $i + 1; //实际的Byte数计1个
            $n = $n + 0.5; //小写字母和半角标点等与半个高位字符宽...
        }
    }
    if ($str_length > $i) {
        $returnstr = $returnstr . "..."; //超过长度时在尾处加上省略号
    }
    return $returnstr;
}
分类: PHP 标签: ,

PHP将汉字转化成拼音的函数(用于取首字母索引或按字母排序中文)

2011年2月23日 admin 没有评论

PHP将汉字转化成拼音的函数(用于取首字母索引或按字母排序中文)
阅读全文…

分类: PHP 标签: , , ,

PHP获取当前页面url

2011年2月23日 admin 没有评论
1
2
3
4
5
6
7
8
9
function get_url() { 
	if (!isset($_SERVER['REQUEST_URI'])) {  
		$url = $_SERVER['REQUEST_URI'];      
		}else{ 
			$url = $_SERVER['SCRIPT_NAME'];     
			$url .= (!empty($_SERVER['QUERY_STRING'])) ? '?' . $_SERVER['QUERY_STRING'] : '';           
			}    
			return $url;
	}

此函数返回正在执行的文件名信息。
当前 url:”http://”.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']

获取URL相关的服务器环境变量:
1,$_SERVER["QUERY_STRING"]
说明:查询(query)的字符串

2,$_SERVER["REQUEST_URI"]
说明:访问此页面所需的URI

3,$_SERVER["SCRIPT_NAME"]
说明:包含当前脚本的路径

4,$_SERVER["PHP_SELF"]
说明:当前正在执行脚本的文件名

实例:
1,http://www.wenan8.com/ (直接打开主页)
结果:
$_SERVER["QUERY_STRING"] = “”
$_SERVER["REQUEST_URI"] = “/”
$_SERVER["SCRIPT_NAME"] = “/index.php”
$_SERVER["PHP_SELF"] = “/index.php”

2,http://www.wenan8.com/?s=55 (附带查询)
结果:
$_SERVER["QUERY_STRING"] = “s=55″
$_SERVER["REQUEST_URI"] = “/?s=55″
$_SERVER["SCRIPT_NAME"] = “/index.php”
$_SERVER["PHP_SELF"] = “/index.php”

3,http://www.wenan8.com/index.php?s=55&l=wenan
结果:
$_SERVER["QUERY_STRING"] = “s=55&l=wenan”
$_SERVER["REQUEST_URI"] = “/index.php?s=55&l=wenan”
$_SERVER["SCRIPT_NAME"] = “/index.php”
$_SERVER["PHP_SELF"] = “/index.php”

$_SERVER["QUERY_STRING"]获取查询语句,实例中可知,获取的是?后面的值
$_SERVER["REQUEST_URI"] 获取http://www.wenan8.com后面的值,包括/
$_SERVER["SCRIPT_NAME"] 获取当前脚本的路径,如:index.php
$_SERVER["PHP_SELF"] 当前正在执行脚本的文件名

分类: PHP 标签: ,

PHP中使用JSON

2011年1月19日 admin 没有评论

目前,JSON已经成为最流行的数据交换格式之一,各大网站的API几乎都支持它。

从5.2版本开始,PHP原生提供json_encode()和json_decode()函数,前者用于编码,后者用于解码。

一、json_encode()

该函数主要用来将数组和对象,转换为json格式。先看一个数组转换的例子:

      $arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
 
      echo json_encode($arr);

结果为

      {"a":1,"b":2,"c":3,"d":4,"e":5}

再看一个对象转换的例子:

      $obj->body = 'another post';
 
      $obj->id = 21;
 
      $obj->approved = true;
 
      $obj->favorite_count = 1;
 
      $obj->status = NULL;
 
      echo json_encode($obj);

结果为

      {
        "body":"another post",
 
        "id":21,
 
        "approved":true,
 
        "favorite_count":1,
 
        "status":null
      }

阅读全文…

分类: PHP 标签: , , ,

PHP利用门户网站API转换IP地址到真实地址

2011年1月14日 admin 没有评论

腾讯,网易,新浪,搜狐都提供了IP地址转换为实际地址的接口.
腾讯:http://fw.qq.com/ipaddress 返回格式为 javascript格式对象. 形如: var IPData = new Array(“124.204.70.160″,”",”北京市”,”");
可以在页面中引入js文件 直接调用。


1
2
<script language="javascript" type="text/javascript" src="http://fw.qq.com/ipaddress"  charset="gb2312"> </script>
<script>document.write("你的IP是:"+IPData[0]+",来自:"+IPData[2]);</script>

搜狐: http://pv.sohu.com/cityjson 也是浏览器 js 调用的。返回 var returnCitySN = {“cip”: “124.204.70.160″, “cid”: “110000″, “cname”: “北京市”}; 的格式。

新浪的IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js
返回
var remote_ip_info = {“ret”:1,”start”:”124.205.0.0″,”end”:”124.205.136.255″,”country”:”\u4e2d\u56fd”,”province”:”\u5317\u4eac”,”city”:”\u5317\u4eac”,”district”:”",”isp”:”\u7535\u4fe1\u901a”,”type”:”",”desc”:”"};

新浪多地域测试方法:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=124.204.70.160

网易有道IP地址查询:http://www.youdao.com/smartresult-xml/search.s?type=ip&q=124.204.70.160

注: JavaScript调用还是PHP调用都要转换一下编码,不然得到的是乱码。
阅读全文…

分类: PHP 标签: , ,

PHP+AJAX 实现星级评分(百度文库评分)

2011年1月11日 admin 1 条评论

好多网站都有评分模块,百度文库,淘宝评价等,现以百度文库为例,简单实现一个基于jQuery的AJAX评分程序。

HTML代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<div id="header">
 
</div>
 
<div id="main">
  <p><span id="s" class="s"></span><span id="g" class="g"></span></p>
  <div class="rate">
 
     <div class="big_rate">
        <span rate="很差" rscore="2" title="www.shaiblog.com 博客导航">&nbsp;</span>
        <span rate="较差" rscore="4" title="www.shaiblog.com 博客导航">&nbsp;</span>
        <span rate="还行" rscore="6" title="www.shaiblog.com 博客导航">&nbsp;</span>
        <span rate="推荐" rscore="8" title="www.shaiblog.com 博客导航">&nbsp;</span>
        <span rate="力荐" rscore="10" title="www.shaiblog.com 博客导航">&nbsp;</span>
        <div style="width:45px;" class="big_rate_up"></div>
     </div>
     <p><span id="q" class="q"></span></p>
     <div id="my_rate"></div>
  </div>
</div>

阅读全文…

分类: PHP 标签: , , , ,

PHP实现时间转换函数

2011年1月7日 admin 1 条评论

之前写过一篇 《将文字表述的时间 转化为相应的时间》 是将文字描述的时间转换为 相应时间, 现在是将时间转换为文字描述。

我在微博或其他一些论坛等发帖子后,会看到发表时间显示为“刚刚”、“1分钟前”、“昨天8:00”等,而不是显示具体日期和时间。

将介绍如何实现基于时间轴的时间的转换。
time():返回当前的 Unix 时间戳

date():格式化一个本地时间/日期。

应用举例:

date(“Y-m-d H:i:s”,time());

格式化当前时间,输出:2011-01-07 15:27:35

strtotime():将任何英文文本的日期时间描述解析为 Unix 时间戳。

应用举例:

echo strtotime(“+1 day”), “\n”;

输出1天前的时间戳:1294474872

date_default_timezone_set():设定要用的默认时区。

一般我们设置北京时间:date_default_timezone_set(“PRC”);

理解上面几个函数后我们来写时间轴函数:

该函数的原理就是将系统当前时间与目标时间比较,得到一个差值,再将差值与时间范围(转换成秒)比较,根据其处在时间轴的范围输出不同的结果(如:5分钟前)。为了便于计算,我们将时间都转换成Unix时间戳。

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
function tranTime($time) { 
    $rtime = date("m-d H:i",$time); 
    $htime = date("H:i",$time); 
 
    $time = time() - $time; 
 
    if ($time < 60) { 
        $str = '刚刚'; 
    } 
    elseif ($time < 60 * 60) { 
        $min = floor($time/60); 
        $str = $min.'分钟前'; 
    } 
    elseif ($time < 60 * 60 * 24) { 
        $h = floor($time/(60*60)); 
        $str = $h.'小时前 '.$htime; 
    } 
    elseif ($time < 60 * 60 * 24 * 3) { 
        $d = floor($time/(60*60*24)); 
        if($d==1) 
           $str = '昨天 '.$rtime; 
        else 
           $str = '前天 '.$rtime; 
    } 
    else { 
        $str = $rtime; 
    } 
    return $str; 
}
分类: PHP 标签: ,

file_get_contents获取搜狐博客乱码问题

2010年12月3日 admin 没有评论

file_get_contents — 将整个文件读入一个字符串
说明
string file_get_contents ( string filename [, bool use_include_path [, resource context [, int offset [, int maxlen]]]] )

一般遇到的乱码问题是gbk与utf-8 之间转换的问题,这个用 php 的 mb_convert_encoding 可以解决, 搜狐的抓出来却是乱码。 google了一下, 搜狐页面编码格式的问题。
看搜狐的header
Accept-Encoding: gzip,deflate

用一个函数 解压就可以了。 ^_^

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
function gzdecode($data) { 
  $len = strlen($data); 
  if ($len < 18 || strcmp(substr($data,0,2),"\x1f\x8b")) { 
    return null;  // Not GZIP format (See RFC 1952) 
  } 
  $method = ord(substr($data,2,1));  // Compression method 
  $flags  = ord(substr($data,3,1));  // Flags 
  if ($flags & 31 != $flags) { 
    // Reserved bits are set -- NOT ALLOWED by RFC 1952 
    return null; 
  } 
  // NOTE: $mtime may be negative (PHP integer limitations) 
  $mtime = unpack("V", substr($data,4,4)); 
  $mtime = $mtime[1]; 
  $xfl   = substr($data,8,1); 
  $os    = substr($data,8,1); 
  $headerlen = 10; 
  $extralen  = 0; 
  $extra     = ""; 
  if ($flags & 4) { 
    // 2-byte length prefixed EXTRA data in header 
    if ($len - $headerlen - 2 < 8) { 
      return false;    // Invalid format 
    } 
    $extralen = unpack("v",substr($data,8,2)); 
    $extralen = $extralen[1]; 
    if ($len - $headerlen - 2 - $extralen < 8) { 
      return false;    // Invalid format 
    } 
    $extra = substr($data,10,$extralen); 
    $headerlen += 2 + $extralen; 
  } 
 
  $filenamelen = 0; 
  $filename = ""; 
  if ($flags & 8) { 
    // C-style string file NAME data in header 
    if ($len - $headerlen - 1 < 8) { 
      return false;    // Invalid format 
    } 
    $filenamelen = strpos(substr($data,8+$extralen),chr(0)); 
    if ($filenamelen === false || $len - $headerlen - $filenamelen - 1 < 8) { 
      return false;    // Invalid format 
    } 
    $filename = substr($data,$headerlen,$filenamelen); 
    $headerlen += $filenamelen + 1; 
  } 
 
  $commentlen = 0; 
  $comment = ""; 
  if ($flags & 16) { 
    // C-style string COMMENT data in header 
    if ($len - $headerlen - 1 < 8) { 
      return false;    // Invalid format 
    } 
    $commentlen = strpos(substr($data,8+$extralen+$filenamelen),chr(0)); 
    if ($commentlen === false || $len - $headerlen - $commentlen - 1 < 8) { 
      return false;    // Invalid header format 
    } 
    $comment = substr($data,$headerlen,$commentlen); 
    $headerlen += $commentlen + 1; 
  } 
 
  $headercrc = ""; 
  if ($flags & 1) { 
    // 2-bytes (lowest order) of CRC32 on header present 
    if ($len - $headerlen - 2 < 8) { 
      return false;    // Invalid format 
    } 
    $calccrc = crc32(substr($data,0,$headerlen)) & 0xffff; 
    $headercrc = unpack("v", substr($data,$headerlen,2)); 
    $headercrc = $headercrc[1]; 
    if ($headercrc != $calccrc) { 
      return false;    // Bad header CRC 
    } 
    $headerlen += 2; 
  } 
 
  // GZIP FOOTER - These be negative due to PHP's limitations 
  $datacrc = unpack("V",substr($data,-8,4)); 
  $datacrc = $datacrc[1]; 
  $isize = unpack("V",substr($data,-4)); 
  $isize = $isize[1]; 
 
  // Perform the decompression: 
  $bodylen = $len-$headerlen-8; 
  if ($bodylen < 1) { 
    // This should never happen - IMPLEMENTATION BUG! 
    return null; 
  } 
  $body = substr($data,$headerlen,$bodylen); 
  $data = ""; 
  if ($bodylen > 0) { 
    switch ($method) { 
      case 8: 
        // Currently the only supported compression method: 
        $data = gzinflate($body); 
        break; 
      default: 
        // Unknown compression method 
        return false; 
    } 
  } else { 
    // I'm not sure if zero-byte body content is allowed. 
    // Allow it for now...  Do nothing... 
  } 
 
  // Verifiy decompressed size and CRC32: 
  // NOTE: This may fail with large data sizes depending on how 
  //       PHP's integer limitations affect strlen() since $isize 
  //       may be negative for large sizes. 
  if ($isize != strlen($data) || crc32($data) != $datacrc) { 
    // Bad format!  Length or CRC doesn't match! 
    return false; 
  } 
  return $data; 
}

PHP导出Excel(一)

2010年11月4日 admin 没有评论

PHP导出Excel方法很多,网上也有很多成熟的类,现在用最简单的方法导出一个Excel, 输出一个Table,然后用header 类型改为Excel就可以.

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
<?php
includer_once("config.php");
$sql="select * from user";
$result=mysql_query($sql);
$data_array=mysql_fetch_array($result);
$keynames=array("orderTrade"=>"订单号","orderRecName"=>"收货人姓名","orderCount"=>"数量","orderPhone"=>"电话","orderRecMail"=>"Email","orderRecAddress"=>"地址","orderRecZip"=>"邮编","receiving"=>"收货时间");
function down_xls($data_array,$keynames,$filename) {
   //组合表头行,以制表符\t分隔,并转码为gb18030
   $title = implode("\t", array_values($keynames));
   $title = iconv("UTF-8","GB18030", $title);
 
   $result[] = $title;
  //组合表体行
   foreach ($data_array as $data){
    //按行组合数据体
      $a1 = array();
      foreach ($keynames as $k => $v){
         if ($k == "endtime"){
            $data[$k] = date("Y-m-d H:i", $data[$k]);
         }
         //utf8转码到gb18030,为了windows正常显示
         $a1[] = iconv("UTF-8","GB18030", preg_replace("/[\t]+/"," ",$data[$k]));
      }
      //以制表符分隔每列
      $result[] = implode("\t", $a1);
   }
 
  //为了windows下正常显示,以\r\n为换行符,分隔数据字符串
   $result = implode("\r\n", $result);
   header("Content-type: text/html; charset=GB18030");
   header("Content-Disposition: attachment; filename=\"".$filename.".xls\"");
   exit($result);
} 
down_xls($data_array,$keynames,"dataxls"); 
?>
分类: PHP 标签: , ,

将文字表述的时间 转化为相应的时间

2010年11月3日 admin 没有评论
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
     /*
	 *将文字表述的时间 转化为相应的时间
	*/
	function changToDateTime($str)
	{
		$str=trim($str);
		$res=array();
		//如果本身是时间格式
		if(preg_match("/(\d{4}-\d{1,2}-\d{1,2})\s(\d{1,2}:\d{1,2}:\d{1,2})/",$str)){
			$res=explode(" ",$str);
			$res[2]=$str;
		}elseif(preg_match('/(\d{4})年(\d{1,2})月(\d{1,2})日\s(\d{1,2}:\d{1,2})/',$str,$match)){
			$res[0]=$match[1].'-'.$match[2].'-'.$match[3];
			$res[1]=$match[4].':00';
			$res[2]=$res[0].' '.$res[1];			
		}elseif(preg_match('/(\d{1,2})月(\d{1,2})日\s(\d{1,2}:\d{1,2})/',$str,$match)){
			$res[0]=date("Y").'-'.$match[1].'-'.$match[2];
			$res[1]=$match[3].':00';
			$res[2]=$res[0].' '.$res[1];			
		}elseif(preg_match('/(\d{4})\s*年(\d{1,2})\s*月(\d{1,2})\s*日/',$str,$match)){
			//只有日期数据的格式
			$res[0]=$match[1].'-'.$match[2].'-'.$match[3];
			$res[1]='00:00:00';
			$res[2]=$res[0].' '.$res[1];	
		}elseif(preg_match("/(\d+)\s*秒前/",$str,$match)){
			$dateTime=time()-$match[1];
			$dateTime=date("Y-m-d H:i:s",$dateTime);
			$res=explode(" ",$dateTime);
			$res[2]=$dateTime;			
		}elseif(preg_match("/(\d+)\s*分钟前/",$str,$match)){//如果是分钟格式 如:3分钟前
			$dateTime=time()-$match[1]*60;
			$dateTime=date("Y-m-d H:i:s",$dateTime);
			$res=explode(" ",$dateTime);
			$res[2]=$dateTime;
		}elseif(preg_match("/(\d+)\s*小时前/",$str,$match)){//如果是小时格式
			$dateTime=time()-$match[1]*3600;
			$dateTime=date("Y-m-d H:i:s",$dateTime);
			$res=explode(" ",$dateTime);
			$res[2]=$dateTime;
		}elseif(preg_match("/(\d+)\s*天前/",$str,$match)){
			$dateTime=time()-$match[1]*3600*24;
			$dateTime=date("Y-m-d H:i:s",$dateTime);
			$res=explode(" ",$dateTime);
			$res[2]=$dateTime;
		}elseif(preg_match("/(\d+)\s*[h|H]ours/",$str,$match)){
			//英文版本的多少个小时以前
			$dateTime=time()-$match[1]*60;
			$dateTime=date("Y-m-d H:i:s",$dateTime);
			$res=explode(" ",$dateTime);
			$res[2]=$dateTime;
		}elseif(preg_match("/([a-zA-Z])+\s+(\d{1,2}):(\d{1,2}:\d{1,2})\s+[A|P]M/",$str,$match)){
			//英文版本带昨天 之类 的说明 
			if($match[1]=='Yesterday'){
				$res[0]=date("Y-m-d",time()-86400);
			}else{
				$res[0]=date("Y-m-d"); //默认设置为今天 会出错!!根据 实际 情况 完善
			}
			//如果 是下等,时间要加12个小时
			if($match[4]=='PM'){
				$match[2]=intval($match[2]); //应该是去年前面 的0! 待改
				$match[2]+=12;
			}
			$res[1]=$match[2].":".$match[3];			
			$res[2]=$res[0]." ".$res[1];
		}elseif(preg_match("/\d{1,2}\/\d{1,2}\/10/",$str,$match)){
			$res[0]="2010-".$match[1]."-".$match[2];
			$res[1]="00:00:00"; 
			$res[2]=$res[0]." ".$res[1];
		}elseif(preg_match('/(\d+)\s*[M|m]inutes/',$str,$match)){
			$dateTime=time()-$match[1]*60;
			$dateTime=date("Y-m-d H:i:s",$dateTime);
			$res=explode(" ",$dateTime);
			$res[2]=$dateTime;
		}elseif(preg_match('/前天 (\d{1,2}:\d{1,2})/',$str,$match)){
			$dateTime=time()-24*60*60*2;
			$dateTime=date("Y-m-d",$dateTime).' '.$match[1].':00';
			$res=explode(" ",$dateTime);
			$res[2]=$dateTime;
		}elseif(preg_match('/昨天 (\d{1,2}:\d{1,2})/',$str,$match)){
			$dateTime=time()-24*60*60;
			$dateTime=date("Y-m-d",$dateTime).' '.$match[1].':00';
			$res=explode(" ",$dateTime);
			$res[2]=$dateTime;
		}elseif(preg_match('/今天 (\d{1,2}:\d{1,2})/',$str,$match)){
			$dateTime=date("Y-m-d").' '.$match[1].':00';
			$res=explode(" ",$dateTime);
			$res[2]=$dateTime;
		}elseif(preg_match('/(\d{1,2}-\d{1,2}) (\d{1,2}:\d{1,2})/',$str,$match)){
			$dateTime=date("Y").'-'.$match[1].' '.$match[2].':00';
			$res=explode(" ",$dateTime);
			$res[2]=$dateTime;
		}elseif(preg_match('/(\d{2,4}-\d{1,2}-\d{1,2}) (\d{1,2}:\d{1,2})/',$str,$match)){
			$dateTime=$match[1].' '.$match[2].':00';
			$res=explode(" ",$dateTime);
			$res[2]=$dateTime;
		}elseif(preg_match('/(\d+)\s*Seconds/',$str,$match)){
			$dateTime=time()-$match[1];
			$dateTime=date("Y-m-d H:i:s",$dateTime);
			$res=explode(" ",$dateTime);
			$res[2]=$dateTime;
		}elseif(preg_match('/([a-zA-Z]+)\s+(\d+).*?(\d{4})/',$str,$match)){
			//格式:Mar 1,2010
			$m=$this->monthSimple($match[1]);
			$res[0]=$match[3].'-'.$m.'-'.$match[2];
			$res[1]="00:00:00";
			$res[2]=$res[0]." ".$res[1];
		}elseif($str=='今天'){
			$res[0]=date("Y-m-d");
			$res[1]="00:00:00";
			$res[2]=$res[0]." ".$res[1];
		}elseif($str=='昨天') {
			$dateTime=time()-86400;
			$res[0]=date("Y-m-d",$dateTime);
			$res[1]="00:00:00";
			$res[2]=$res[0]." ".$res[1];
		}elseif($str=='前天'){
			$res[0]=date("Y-m-d",(time()-86400*2));
			$res[1]="00:00:00";
			$res[2]=$res[0]." ".$res[1];
		}elseif($str=='刚刚'){
			$res[0]=date("Y-m-d");
			$res[1]=date("H:i:s");
			$res[2]=$res[0]." ".$res[1];
		}else{ 
			$res[0]="0000-00-00";
			$res[1]="00:00:00";
			$res[2]=$res[0]." ".$res[1];
		}
		return $res;
	}
分类: PHP 标签: ,