Jump to content

regex


purencool

Recommended Posts

I have been playing with reg ex patterns for the last couple of hours. I realized I have been coding in security a hole. But for the life of me I don't know the answer.

 

In preg match it will return a 1 true. So the code below is saying if you find anything that is not one of these values the condition is true. Meaning anything but a-zA-Z true is this correct?

 

How do I make it so if you find anything but a-zA-Z return false?

if ( preg_match('/[a-zA-Z]/', $_GET['contact'] )== 1)

 

 

 

Link to comment
Share on other sites

Hi, you do that by using the not operator before you character specifications ^:

 

if ( preg_match('/[^a-zA-Z]/', $_GET['contact'] )== 1)

 

also, you can use i to indicate case insensitivty so you don't need to put a-zA-Z (though I do):

 

if ( preg_match('/[^a-z]/i', $_GET['contact'] )== 1)

 

Note, the 'i' comes after your delimiter.

 

Hope that helps!

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.