I am working on a project that involves a type of caching to be performed. Multiple caches can be done for situations based on different cache names. In the files I am storying a cache like so:
{cache:2011-12-11 02:01:47}
And when I search for it, I am trying to preg_match it like this:
$match = "{cache:/\d{4}\-\d{2}\-\d{2} \d{2}:\d{2}:\d{2}/}";
$str = 'FIND ME! {cache:2011-12-11 02:01:47}';
if (preg_match($match, $str, $matches)) {
print "it's a match";
print_r($match);
}
The problem is, it never finds it. But this will work if I do:
$match = "/\d{4}\-\d{2}\-\d{2} \d{2}:\d{2}:\d{2}/";
What am I doing wrong with my preg_match statement? And is there something type of string search I could use that is faster than preg_match?
Your in-code regex will not work, because you copy&pasted the delimiters where they don’t belong:
This way your
{and}became the regex delimiters, and the inner slashes were interpreted as literal characters to search for.It rather should have been:
Note also the escaped leading
\{curly.