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
). I tried preg_match
and was unable to only get that portion of the text.
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("/(\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";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.