Jump to content

Recommended Posts

$me = 'cheese';

if(preg_replace('/[a-zA-Z0-9]/', '', $me)){

  echo "Bad Characters";

}

else

{

  echo "Passed";

}

 

-Keep getting it passed when the string cheese has letters in it which should fail the test. Any thoughts?

Maybe you are looking for the preg_match() function instead of the preg_replace(). What your doing is if the preg_replace() function returns true (replacing your letters with nothing), it will echo out bad characters.

Your code didn't work like I expected it to either...  But I've never actually used preg_replace so that's not saying much.

 

You could try:

 

$string = 'cheese';
$pattern = '/[a-zA-Z0-9]/';
echo preg_match($pattern, $string) ? 'matched' : 'not matched';

This will show that the string matched.

 

Hope it helps.

behicthebuilder, thank you very much. This worked very well.  :D

 

$pattern = '/[~!#$%^&*()+=|:";\'<>,/*+]/';
$check = preg_match($pattern, $a) ? 1 : 0;
if ($check == 1){
$error = true;
$status = "Please only use Alphanumeric characters (A-Z, a-z, 0-9). Underscores and dashes are allowed (_,-).";
}
if ($check == 0){
//no illegal characerts found
}

 

Now, in my blocked characters list, would anyone know how could I add {}[] without the script thinking that i'm closing the encapsulating [ ]'s ?

 

 

Escape them with a slash ( \ ).

 

If you are going to that trouble, why not just check to see if the string contains only alphaNumeric + underscore and dashes.

 

$pattern = '~^[a-zA-Z0-9\s_\-]+$~'; //Matches any string with spaces, underscore, dashes, or alphanumeric.

I've tried to find a good how-to for how to add characters to this but I haven't had luck. Even what you posted:

 

'~^[a-zA-Z0-9\s_\-]+$~'

 

Why 2 ~, why come in brackets and others not, why \s_ ?

 

I'm having problems grasping the logic of this statement  :-\

 

On the other hand! I followed your advice and checking to make sure that the fields only contain your string. Which works fine. I just hate to not understand why it works.

 

Thanks!

The (~) is de-limiters, It is like yours except you are using slashes.  Only when delimiting the slashes, you must escape the escaping slash.  Just adds more slashes, like (\\{).

 

I'm probably not being clear.

 

Explanation of the string.

~ delimiter

^ matches beginning of string

[] encloses a set.

a-z matches any lowercase alpha character

A-Z matches any uppercase alpha character

\s matches any space

_ matches any underscore

\- matches any dash

+ matches 1 or more of the previous expression

$ matches the end of the string

~ ending delimiter.

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.