Jump to content

Beginner: preg_match simple query (I hope)


a1ias

Recommended Posts

Been playing with this since early this fine Sunday morning and have decided to turn to the pro's for a little pointer.

 

The code I am having an issue with is as follows:-

 

<?php
$checkthis = 'a41$;wilsond';

if (preg_match('[^a-zA-Z0-9]', $checkthis)) {

  $checked = 'Bad String';

  } else {

  $checked = 'Good String';
}

echo $checked;

?>

 

What I am expecting that snippet to do is check the variable $checkthis for alphanumeric characters only, and since it contains both a $ and a ; I expect $checked to be returned as 'Bad String', which appears not to be the case...doh!

 

I have no doubt that I have made a simple beginners error, but am failing to understand why it does not work.

 

Any pointers are greatly appreciated.

 

 

a1ias

Link to comment
Share on other sites

Many many thanks for the help. I've also discovered that

if (preg_match('/[^a-zA-Z0-9]/', $checkthis))

works.

 

Is there a big difference between either of them and, as any documentation I have on regex is sketchy to say the least, what do those delimiters actually represent, if I may ask?

Link to comment
Share on other sites

The delimiters can be be any non-alphanumeric character. Some people use slashes, some use other characters. You just need to use the same leading and trailing delimiter. It simply denotes the start and end of the pattern, so the one I posted and the one you posted after are effectively identical.

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.