Jump to content

[SOLVED] ereg and preg_match accepting dollar sign ?


the_shadow

Recommended Posts

Hi all,

 

I'm new to using regex, but basically I just want to verify that a variable contains either letters or numbers and nothing else (had my own function, but thought there would be any easier way of doing this). I've searched around and came up with these expressions:

 

ereg("^[A-Za-z0-9]*$", $test)

preg_match("/[a-zA-Z0-9]{0,}/",$test)

 

 

They both seem to work alright except the only problem I'm having is that both of them accept a dollar sign ($) after a number and before a letter as well as between letters in the $test string.

 

$test = "a324$sfs32sf";

//$test = "a324s$fs32sf"; // also accepts the $ sign

 

if (ereg("^[A-Za-z0-9]*$", $test)) {

  echo "ereg";

}

if (preg_match("/[a-zA-Z0-9]{0,20}/",$test)) {

  echo "pregmatch";

}

 

 

Can anyone offer some suggestions as to why this is happening?

Thank you.

Link to comment
Share on other sites

$test = "a324$sfs32sf";

 

echo "hi$bob";

 

will echo:

hi

 

it will also echo a warning on a variable that has not been set with is $bob, if you have your warnings turned on

 

do you see why?

using the $ in double quotes it will try to echo the variable

 

change it to:

$test = 'a324$sfs32sf'

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.