Jump to content

Recommended Posts

Alphanumeric:

<?php
function is_alphanumeric($string) {
return !preg_match('#[^A-Za-z0-9]#', $string);
}

 

Alphabetical:

<?php
function is_alphabetical($string) {
return !preg_match('#[^A-Za-z]#', $string);
}

 

whoa there buddy, step away from the regex.  There's no reason to be doing double negatives like that. ^ inside a character class will only match something that is not listed, so you're having to turn around and use ! to do the opposite of that negative.

 

Also, you're only checking for 1 character.  The first character.  So if a string is '1abc' it will return positive for numeric. You need to add a quantifier and start and end of string assertion.

 

if preg_match('~^[A-Za-z]+$~', $string)
   // is alpha
if preg_match('~^[A-Za-z0-9]+$~', $string)
   // is alphanumeric

 

you could also use ctype_digit ctype_alnum ctype_alpha

Actually, give it a try, it will search every character and function as expected until it finds a match (which it shouldn't, because matches are bad in this case), you should know that! But you're right about the double negatives, it can be confusing. As far as your solution goes, I've seen an example where a parser will stop at a new line character that matches between the start-line and end-line character. Eg. "test\n@" will return true for your alphanumeric pattern. I doubt that's true in this case, but I didn't feel up to trying it... hell, I could be mistaken, but I went with what I knew would work.

Actually, give it a try, it will search every character and function as expected until it finds a match (which it shouldn't, because matches are bad in this case), you should know that! But you're right about the double negatives, it can be confusing. As far as your solution goes, I've seen an example where a parser will stop at a new line character that matches between the start-line and end-line character. Eg. "test\n@" will return true for your alphanumeric pattern. I doubt that's true in this case, but I didn't feel up to trying it... hell, I could be mistaken, but I went with what I knew would work.

 

well damn...upon further inspection, I was wrong even about double negative.  Apparently 2 wrongs do make a right.  It does search until it finds a match. So if you match

 

return !preg_match('#[^A-Za-z]#', $string);

 

against

asdf

2asdf

sdf2

2323

you will get

 

false

true

true

true

 

from the negated class, but the ! will invert it.  Okay my bad, it does work.  But still, you don't need to make it so complicated, lol.  As far as mine returning a true on a new line:

 

$string = "test\n@";

if (preg_match('~^[A-Za-z]+$~', $string))
   echo "$string true";
else
   echo "$string false";
echo "<br/>";
if (preg_match('~^[A-Za-z0-9]+$~', $string))
   echo "$string true";
else
   echo "$string false";

 

output:

 

test @ false

test @ false

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.