[英]Get some text from a string (regular expression)
I have a string like: 我有一个像这样的字符串:
Lorem Ipsum Dolor Sir 2013-10-22 Lorem Ipsum Dolor...
How should I take the date? 我该如何约会? I only want the portion of the text that determine the date ( yyyy-mm-dd
). 我只想要确定日期的文本部分( yyyy-mm-dd
)。 I tried preg_match
and was unable to only get that portion of the text. 我尝试了preg_match
,但无法仅获取文本的那部分。
preg_match('/(\d{4}-\d{2}-\d{2})/', $text, $matches);
echo $matches[1];
\d - any digit
{4} require 4 of the previous match
\d{4} - require 4 digits
Matches inside parenthesis are put in the third argument of preg_match
, so that the first item in it is the whole match and the next are matches inside parenthesis. 括号内的匹配项放在preg_match
的第三个参数中,因此其中的第一项是整个匹配项,下一项是括号内的匹配项。
preg_match("/(\d{4}-\d{2}-\d{2})/", $text, $regs);
echo $regs[0]; // The whole match is in $regs[0]
echo $regs[1]; // The match inside the 1. parenthesis is in $regs[1]
You can use this regex: 您可以使用此正则表达式:
$str = 'Lorem Ipsum Dolor Sir 2013-10-22 Lorem Ipsum Dolor...';
if (preg_match('/(\d{4}-\d{2}-\d{2})/', $str, $match))
echo $match[1] . "\n";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.