Jump to content


Photo

simple preg_match help needed


  • Please log in to reply
1 reply to this topic

#1 amazinggrace1983

amazinggrace1983
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 22 August 2006 - 04:08 PM

This will probably take 2 seconds for anyone else, but I've been thrown in the deep end so please help.
Basically, I create a string (its a file name), called file, which has the form ddmmmyy_number of anodes, eg. 22aug06_twoanodes or 03jul06_allanodes.

I need to check the file names are of the right format, using preg_match. Can anyone please tell me what needs to go in it?? I know it's something like

preg_match("/[\0123]\d\$month\[0][6789]\_$ANODE$/", $file)

but can I really just use $month and _$ANODE?? (these are arrays, created already)

Please please help. Very urgent!!!!
Grace 

#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 22 August 2006 - 04:32 PM

This does not consider invalid dates.

<pre>
<?php

	$months = array(
		'jan', 'feb', 'mar', 'apr', 'may', 'jun',
		'jul', 'aug', 'sep', 'oct', 'nov', 'dec'
	);
	$anodes = array(
		'two', 'all'
	);

	$tests = array(
		'22aug06_twoanodes', 
		'03jul06_allanodes' ,
		'01xxx08_noanodes',
		'invalid_string',
	);
	
	foreach ($tests as $test) {
		echo "<b>Checking $test...</b><br />";
		if (!preg_match('/^\d{2}([a-z]{3})\d{2}_(.+?)anodes$/', $test, $matches)) {
			echo "Invalid file name.<br />";
		}
		if (! in_array($matches[1], $months)) {
			echo "Invalid month.<br />";
		}
		if (! in_array($matches[2], $anodes)) {
			echo "Invalid anode type.<br />";
		}
	}
?>
</pre>

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users