Jump to content

[SOLVED] preg_match just ignores all patterns? But why?


Recommended Posts

Hi I'm clearly mixied up in my logic. I'm try to see if there is a match with any unwanted characters - but preg_match simply ignores my seach parameters - so I have made a mistake somewhere  . . . but where? Can you help me?

 

I get the same result for these two differing inputs : -

 

$_POST['branch'] = !@#$%^&

or

$_POST['branch'] = Melrose Arch

 

if(preg_match('/[^a-zA-Z0-9_'-]/',$_POST['branch'] )) {
         global $errors;
                  $errors[] = "Invalid code in Branch name";
            }

HI, thanks. I've tested your suggestion and it seems to make no difference.

 

Also I understand that the "^" needs to be inside like this [^a-zA-Z0-9] , as opposed to ^[a-zA-Z0-9] . . is this correct?

 

Also, my understanding of the "$" sign is to have the pattern matched at the end of a string . . . is this also correct?

 

??? David

 

 

HI thanks for the comment.

 

I want to identify if there are any characters that should not be there - like < ' " !@#$%^& etc.

 

If the $_POST['branch '] has any of these, I'll assume that someone is messing with the input and exclude the post.

 

So I assumed that [^a-zA-Z0-9] would match any thing that is NOT a letter or a number.

 

 

 

What I gave you should work. Here's some examples:

 

$str = 'test)';
$pattern = "/^[a-zA-Z0-9_'-]*$/";
if(preg_match($pattern, $str, $matches)) echo 'Validates';
else echo 'Does not validate';

Output: Does not validate

 

$str = 'all_acceptable_characters';
$pattern = "/^[a-zA-Z0-9_'-]*$/";
if(preg_match($pattern, $str, $matches)) echo 'Validates';
else echo 'Does not validate';

Output: Validates

 

Try it yourself.

 

Something along the lines of:

 

//$_POST['branch'] = '!@#$%^&';
$_POST['branch'] = 'Melrose Arch';
echo (preg_match('#^[a-z0-9_ -]+$#i', $_POST['branch']))? $_POST['branch'] . '= Valid!' : $_POST['branch'] . '= Invalid!';

 

?

 

Granted, I didn't add the single quote inside the character class, since:

I want to identify if there are any characters that should not be there - like < ' " !@#$%^& etc.

 

So I am assuming you only want a-z (upper / lowercase), 0-9, _ and a dash.. I added a space in the character class as well ( since you have the example $_POST['branch'] = Melrose Arch (which should be $_POST['branch'] = 'Melrose Arch' ). I also used the + quantifier instead of *, as I suspect you would want at least a single character minimum (but something tells me an interval would be better: {min, max})

EDIT -  Looking at your original line:

[font=courier new][size=12px]if(preg_match('/[^a-zA-Z0-9_'-]/',$_POST['branch'] )) {[/size][/font]

Note that since you are using single quotes to encapsulate your pattern (which is perfectly fine), you would need to escape that quote inside the character class (even though you don't seem to want the quote as I quoted you at the top of this post). Otherwise, the regex engine thinks you are done with the pattern. And as AlexWD pointed out, ^ and $ are necessary to check from beginning to end of string.

if(preg_match('/[^a-zA-Z0-9_'-]/',$_POST['branch'] )) {
         global $errors;
                  $errors[] = "Invalid code in Branch name";
            }

 

The only problem with that code is that you forgot to escape the single quote (or remove it if you don't want to allow it), as nrg also mentioned. And yeah, you should also add a space inside the character class to allow spaces.

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.