expression help for php newbie

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bordman
Forum Newbie
Posts: 16
Joined: Thu Jan 26, 2006 8:20 am
Location: RI

expression help for php newbie

Post 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!
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
bordman
Forum Newbie
Posts: 16
Joined: Thu Jan 26, 2006 8:20 am
Location: RI

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

Post 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>";}
}
}
}
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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";
}
?>
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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 :)
bordman
Forum Newbie
Posts: 16
Joined: Thu Jan 26, 2006 8:20 am
Location: RI

SWEET!!!

Post 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
Post Reply