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
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'
);
?>

Link to comment
Share on other sites

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'?

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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