例如有一个数据
$ string ="我的生日是2014年7月11日"
如何在字符串中获取日期?
答案 0 :(得分:2)
$string = 'Mr Roux said it will be argued that on July 11 2014 it was physically impossible for Ms Steenkamp to have continued to scream after being struck in the head by a bullet from gun, especially if the sound had to travel 177 metres from the bathroom where the young model cowered.\"A person with that brain damage will have no cognitive response,\" he said.A short time later Dr Burger’s evidence was complete, herself fighting back tears at having to relive the night in question.\"It was awful to hear her shout before the shots,\" she said, saying she was still plagued by the screams. \"Its still quite raw.\" The Oscar Pistorius';
preg_match_all('/\b(?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May?|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sept(?:ember)?|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?) (\d|\d{2}) \d{4}/', $string, $matches);
//flattens the array, because it is bi-dimensional
$flattened_array = new RecursiveIteratorIterator(new RecursiveArrayIterator($matches));
//loop through this flattened array
foreach($flattened_array as $k => $v)
{
//if the value of that array item is not a pure integer (11 or 12, etc..)
if((int) $v === 0)
{
//add the value to the final output array
$final_matches[] = $v;
}
}
var_dump($final_matches);
//returns array(2) { [0]=> string(12) "July 11 2014" [1]=> string(12) "July 12 2014" }
正则表达式将与Jan(uary),Feb(ruary),Mar(ch)等匹配任何内容......后跟空格,然后是X或XX(数字),然后是空格,然后是XXXX。