Jump to content

Recommended Posts

Warning: preg_match(): No ending delimiter '^' found in events.php on line 3

 

I have no idea what that means.

 

<?php
$checkEventMonth = preg_match("^[0-9]+$", $_GET["m"]);
?>

 

Does that check that $_GET["m"] starts with a number and every character after the first is also a number?

Link to comment
https://forums.phpfreaks.com/topic/55047-solved-preg_match-error/
Share on other sites

If someone is going to post a tutorial about how to do something, then IT NEEDS TO BE RIGHT!!!

LOL. Geez. (not anyone here. something i found on-line.)

 

So I _think_ I fixed the error. But now all of my statements are coming up 0 when they are clearly 1.

What am I doing wrong?

<?php

$checkEventMonth = preg_match("/^[0-9]+$/", $_GET["m"]);
$checkEventDay = preg_match("/^[0-9]+$/", $_GET["d"]);
$checkEventYear = preg_match("/^[0-9]+$/", $_GET["y"]);
if(($eventMonth && $eventDay && $eventYear) == 1) {
$eventMonth = $_GET["m"];
$eventDay = $_GET["d"];
$eventYear = $_GET["y"];
$eventDate = date("Y-m-d", mktime(0, 0, 0, $eventMonth, $eventDay, $eventYear));
/*dbconnect();
$query = "SELECT * FROM events WHERE theDate='$eventDate' ORDER BY title ASC";
$result = mysql_query($query) OR DIE("Unable to run event query.");
dbclose();*/
$eventData = "Events for $eventMonth.$eventDay.$eventYear:\n";
} else {
$eventData = "Not a valid date.\n";
}

?>
<?php echo $eventData; ?>

erm

 

im not a regex guy but im not sure your doing it right.

 

preg_match("/^[0-9]+$/", $_GET["m"]);

 

to ME that looks like your only trying to see if "m" has a number between 0 and 9.

 

AND you dont do anything with "$checkEventMonth". you just do the preg_match then leave the vars alone.

 

Im guessing you may mean to do this ?

 

if(($checkEventMonth && $checkEventDay && $checkEventYear) == 1) {

to ME that looks like your only trying to see if "m" has a number between 0 and 9.

 

Not quite, it checks that EVERY character is between 0 and 9.  The ^ and $ anchor the start and end of the string, so the expression only matches if every character from the start to the end is 0-9.  It also checks that there is at least one character (that's done by using + instead of * )

  • 2 weeks later...

Some examples:

 

'|^[0-9]+|' => Match 1 or more digits (Doesn't match empty string)

'|^[0-9]*|' => Match 0 or more digits (DOES match empty string)

'|^[0-9]{5}|' => Match exactly 5 digits

'|^[0-9]{3,5}|' => Match 3, 4 or 5 digits.

'|^[0-9]?|' => Match 0 or 1 digits.  Usually used to say "This character can be there or it can be missing, I don't care".  Equivalent to {0,1}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.