Solved

Matching - newbie to regular expressions

Posted on 2009-07-10
6
160 Views
Last Modified: 2012-05-07
I want to parse the input from a form so that it only contains upper or lower  letters, numbers and hyphens. How do write such an expression?
if (isset($_POST['frmDomName'])) {
	if ('contains something other that legal l characters') {
		header('Location: http://mydom.com/Failure.php');
	}
}

Open in new window

0
Comment
Question by:cescentman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 12

Expert Comment

by:jet-black
ID: 24822242
Add all charachers that you want:
Like:

if (preg_match('/[A-Za-z0-9]/', $_POST['frmDomName']))
{
                header('Location: http://mydom.com/Failure.php');

}
0
 
LVL 5

Expert Comment

by:mnialon
ID: 24822282
hello,
to match upper or lower  letters, numbers and hyphens, use

if (preg_match('/[A-Za-z\-]/', $_POST['frmDomName']))
0
 
LVL 9

Expert Comment

by:xBellox
ID: 24822306
This should work fine:

if (isset($_POST['frmDomName'])) {
        if (!ereg("^([0-9]|[a-z]|[A-Z]|_)+$", $_POST['frmDomName'], $reg)) {
                header('Location: http://mydom.com/Failure.php');
        }
}

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Accepted Solution

by:
jet-black earned 125 total points
ID: 24822343
Sorry, I forgot the begining and ending metacharacters:

if (preg_match('/^[A-Za-z0-9\-]*$/', $_POST['frmDomName']))
{
 
}

Open in new window

0
 
LVL 1

Author Comment

by:cescentman
ID: 24822436
This work Just fine, but it should be noted that the ! is required for the particular logic I outlined.
if (!preg_match('/^[A-Za-z0-9\-]*$/', $_POST['frmDomName'])) {
	header('Location: http://mydom.com/Failure.php');
}

Open in new window

0
 
LVL 12

Expert Comment

by:jet-black
ID: 24822457
cescentman,
Yes, you're right.
0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question