Jump to content

[SOLVED] Preg_match question


Mrwilson1

Recommended Posts

I am trying to validate the following in which the requirement for validation is 3 letters (months) and 1 digit.  What is the proper format?

 

include(isset($_GET['page']) && preg_match('/^\w(3)\d+\.htm$/',

            $_GET['page']) ?

          $_GET['page'] : 'apr7.htm');

 

all file names will appear as the expression "apr7.htm above

 

thanks

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

Below is a very specific example. Your pattern is not working because (3) should be {3} to quantify the \w. And remember that \w includes digits and an underscore. Also, shouldn't you be checking for the file's existence?

 

<?php
include(
	(isset($_GET['page']) && preg_match('/^(?:jan|feb|ma[ry]|apr|ju[nl]|aug|sep|oct|nov|dec)\d+\.htm\z/', $_GET['page'])) ?
		$_GET['page'] : 'apr7.htm'
);
?>

I think checking for the file existance would be appropriate.  But what would I use if I just want to check for a certain number of letters?  In the example you give the file names certainly work in this case, but looking on to other form fields and other files, would

 

('/^/w{3}\.htm\z'

work to just validate that the file name is of a certain amount of letters/numbers?  I guess I am asking if I can use \w{3} as the pattern to match without adding more names such as in your example (thanks BTW)

 

Can you also show me how to check for file existence?  I am assuming that would be an "if (file_exists" statement between this: htm\z/', $_GET['page'?

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.