Page 1 of 1

expression help for php newbie

Posted: Wed Feb 01, 2006 10:13 am
by bordman
In this line of code:

Code: Select all

$result=ereg("^[0-9]+$",$month,$trashed);

can someone explain what this part is doing:

Code: Select all

"^[0-9]+$"


I'm not sure what the ^ or the +$ is doing. Or, if someone has a good reference that would be helpful too.

Thanks!

Posted: Wed Feb 01, 2006 10:17 am
by s.dot
^ means starts with
$ means end with

[0-9]+ is finding a number any amount of times

there's a good tutorial on regular expressions in the 'regex' forum on this site

I don't think that's the problem then.

Posted: Wed Feb 01, 2006 10:41 am
by bordman
Cool, I'll check that out, but now that I know what that does, I don't think that's my problem then.

I have code that check's a date format. Right now the code checks for mm/dd/yyyy and displays an error if somebody puts in 2/1/2006 instead of 02/01/2006. I like to change this piece of code to allow 2/01/2006 or 02/1/2006 or 2/1/2006. The only thing I've changed correctly (I think) is the first error_msg that checks the string length.

Code: Select all

//Check the length of the entered Date value 
if((strlen($strdate)<8)OR(strlen($strdate)>10)){
	$error_msg.=("Please Enter the date in 'mm/dd/yyyy' format.<br>");
} else {
	//The entered value is checked for proper Date format 
	if((substr_count($strdate,"/"))<2){
		$error_msg.=("Enter the date in 'mm/dd/yyyy' format Rob<br>");
		} else {
			$pos=strpos($strdate,"/");
			$month=substr($strdate,0,($pos));
			$result=ereg("^[0-9]+$",$month,$trashed);
			if(!($result)){
				$error_msg.="Policy Start Date: Please Enter a Valid 2 digit Month.<br>";
			} else {
	if(($month<=0)OR($month>12)){
		$error_msg.="Policy Start Date: Please Enter a Valid 2 digit Month.<br>";
		}
}
$date=substr($strdate,($pos+1),($pos));
if(($date<=0)OR($date>31)){$error_msg.="Policy Start Date: Enter a Valid DAY of the month.<br>";}
else{
$result=ereg("^[0-9]+$",$date,$trashed);
if(!($result)){$error_msg.="Policy Start Date: Enter a Valid DAY of the month.<br>";}
}
$year=substr($strdate,($pos+4),strlen($strdate));
$result=ereg("^[0-9]+$",$year,$trashed);
if(!($result)){$error_msg.="Policy Start Date: Enter a Valid year<br>";}
else{
if(($year<1900)OR($year>2200)){$error_msg.="Policy Start Date: Enter a year between 1900-2200<br>";}
}
}
}

Posted: Wed Feb 01, 2006 11:04 am
by pickle
You can use list() and explode() to make your routine a little more flexible:

Code: Select all

<?php
$strdate = "9/2/2004";
list($month,$day,$year) = explode("/",$strdate);
?>
You can then use checkdate() to see if the entered values constitute a valid date or not:

Code: Select all

<?php
$valid_date = checkdate($month,$day,$year);
?>
So, all your code can pretty much be broken down to:

Code: Select all

<?php
list($month,$day,$year) = explode("/",$strdate);
if(!checkdate($month,$day,$year))
{
   $error_msg = "Please enter a valid date.";
}
else if($year < 1900 || $year > 2200)
{
  $error_msg = "Please enter a year between 1900 and 2200";
}
?>

Posted: Wed Feb 01, 2006 11:32 am
by Jenk
First up - Scrotaye, '+' denotes 1 or more, '*' denotes 0, 1 or more occurances. :)

Next up.. for bordman to try:

http://us3.php.net/manual/en/function.strtotime.php

Code: Select all

<?php
if (preg_match('#^\d{1,2}/\d{1,2}/\d{2,4}$#', $strdate)) {
    if (($time = strtotime($strdate)) !== FALSE) { //if you are using phpver < 5.1.0 use !== -1
        echo 'Date entered = ' . date('d/m/y', $time);
    } else {
        echo 'Invalid date entered!';
    }
} else {
    echo 'Invalid date entered!';
}
?>
Hope this helps :)

SWEET!!!

Posted: Wed Feb 01, 2006 12:04 pm
by bordman
Pickle,
Thanks for the help, that works out great!!

Jenk and scrotaye,
Also thanks for the explaination and the tutorials, I've already looked through them and they help it all make sense!

Best Regards,
Bordman