EasterBunnyBugSmasher
EasterBunnyBugSmasher

Reputation: 1582

php preg_match not giving a result?

sorry, I'm not good at php, this might be a very obvious mistake. I'm trying to this:

  $datum = "Samstag, 26.10.2013";
  echo("<p>".$datum."</p>");

  $regresult = preg_match("(.*)([0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,4})",$datum, $matches);
  echo("<p>Result: ".$regresult."</p>");
  echo("<p>Error: ".preg_last_error()."</p>");
  echo("<p>Match: ".$matches[2]."</p>");

and the result is:

<p>Samstag, 26.10.2013</p><p>Result: </p><p>Error: 0</p><p>Match: </p>

but in the documentation of preg_match it says:

preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or FALSE if an error occurred.

anyone know what I'm doing wrong?

Upvotes: 1

Views: 2190

Answers (2)

echochamber
echochamber

Reputation: 1815

You're missing your delimeters.

preg_match requires you to do something like this

preg_match('/{pattern}/', $subject);

In this case I've used / as my delimeter.

An example: match exactly 3 digits

preg_match('/\d{3}/', $subject);

If you check out php's documentation on preg_match you can see other examples

edit:

Php native date time functions reference links:

strtotime

date/time formats

date

time

Upvotes: 4

Ωmega
Ωmega

Reputation: 43673

You have to use delimeters / in regex pattern:

preg_match("/(.*)([0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,4})/", $datum, $matches);
            ↑                                        ↑
        delimeter                                delimeter

See this demo - it works...

Upvotes: 2

Related Questions