Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

escaping slashes in preg_match

Status
Not open for further replies.

dbrb2

Instructor
Jul 19, 2004
121
0
0
GB
Hello,

I'm having some trouble with my regular expressions...
I want to match a date using preg match, where the delimiter may be one of either " ", "/","-","." or "\"
Code:
$dateMatch = '/^\s*([0-9]{1,2})[//// \\\.-]+([0-9]{1,2})[//// \\\.-]*([0-9]{0,4})/';
  
preg_match($dateMatch,$str,$match);
I tried the above patten, with no joy. I think the problem lies with my escaping of forward and back slashes... any ideas?
 
Hi

Use backslash ( \ ) to escape the slashes ( / ) :
Code:
$dateMatch = '/^\s*([0-9]{1,2})[[COLOR=red pink]\[/color]/ \\.-]+([0-9]{1,2})[[COLOR=red pink]\[/color]/ \\.-]*([0-9]{0,4})/';
Or just use a different delimiter for the regular expression :
Code:
$dateMatch = '[COLOR=red pink]#[/color]^\s*([0-9]{1,2})[/ \\.-]+([0-9]{1,2})[/ \\.-]*([0-9]{0,4})[COLOR=red pink]#[/color]';

Feherke.
 
but there are (much) better ways to match a date... for example checkdate() (separate the date parts with split or explode). or use strototime() (or any other method).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top