numeric and alphanumeric


I looked for this in the PHP manual on, but I don't find what I'm looking for (maybe I looked over it).

Basically, what I need is a way to identify whether or not a submitted value contains alphanumeric characters.  It would be even better if those characters could be stripped from the string.

For example, when a user fills in a form field and enters "dfdg2435", the script will return an error because only numbers are allowed.  If it could also remove the "dfdg" part, that would be very nice.

I can always search a string for characters with for instance strstr(), and then even remove the string, but that obviously is no practical solution if you've got a whole collection of ASCII (and other) characters to deal with.

Any suggestions?

Who is Participating?
Richard CoffreConnect With a Mentor e-commerce Product ManagerCommented:

To identify if a string is a numeric or not, you can use is_numeric().

If false you can scan the string and rebuild another one without the letters and other special characters.

Hope it helps.

IconMan7Author Commented:
Yup.  That works perfectly.
I'll try to figure out how to best replace the alphanumeric stuff, but it's not essential.

Richard Coffree-commerce Product ManagerCommented:
You're welcome
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.