Jump to content

Help With Invalid Characters


adam84

Recommended Posts

Hi,

 

I wrote a regex that will look for alphanumeric values only.

/^\w+$/

 

What is happening is I am reading some data from a file and sometimes there is data that is invalid and it shows up as a square box. I am trying to develop a regex statement that will look for a character that is not [a-zA-Z0-9].

 

Any ideas on what I can do to make this work?

 

Thanks

Link to comment
Share on other sites

Pikachu is right, but just for the record, a regex version:

 

<?php
$regex=',[^[:alnum:]],';
echo preg_match($regex,"almost_alnum");
?>

 

The output is 1 because of the underscore in almost_alnum. :)

Link to comment
Share on other sites

I know this is solved but I thought I would give my tuppence worth. You could have used \W to match anything that is not a word character. Just remember this, all the shorthand regex tricks you know, \w \d \s etc etc., they all have exact opposites which are there uppercase versions. I.e.:

 

\W = Match any character that is not a word character

\D = Match any character that is not a digit

\S =  Match any character that is not a white space character etc etc.

 

Make those letters lower case and you need only to remove the word not from the definitions to find they work.

 

Furthermore, you also amusingly almost posted a regex which needed extremely little tweaking to have worked. You were just missing the vital ^ character. Placed within a character class it means match anything that is not the following characters. So you're regex, [a-zA-Z0-9], needed only become, [^a-zA-Z0-9], to have worked.

 

Joe

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.