PHP / Regex如何获取此字符串的值?

时间:2013-04-22 02:37:53

标签: php regex

你好我很确定我能找到能解决这个问题的人。

我希望从这个字符串中获取“2013年4月21日”的值:

$string = "Issue Date: Sunday April 21, 2013 / week 10/2003week 11/2003week 12/2003week 13.."

提前致谢...

3 个答案:

答案 0 :(得分:2)

preg_match()与以下正则表达式/^Issue Date: [A-z]+ (.*) \//一起使用:

// prepare string and pattern
$string = 'Issue Date: Sunday April 21, 2013 / week 10/2003week 11/2003week 12/2003week 13..';
$pattern = '/^Issue Date: [A-z]+ (.*) \//';

// exec regex and check for results
if(!preg_match($pattern, $string, $matches)) {
    die('the pattern was not found');
}

// output the results:
$date = $matches[1];
echo $date; // output: 'April 21, 2013'

答案 1 :(得分:2)

不需要正则表达式:

$string = 'Issue Date: Sunday April 21, 2013 / week 10/2003week 11/2003week 12/2003week 13..';
$array = explode(' ',$string);
$date = $array[3] . ' ' . $array[4] . ' ' . $array[5];
echo $date;

答案 2 :(得分:1)

如果您正在寻找首先出现的内容(在“发行日期:”之后),那么这将有效:

Issue Date: (.*) /