Regual expressions for detecting numeric, alpha numeric and alpha strings

Posted on 2005-04-14
Medium Priority
Last Modified: 2012-08-13
Hi Everyone,

I wrote the following patterns to use in eregi():

- For detecting if the string is totally numeric

- For detecting if the string is totally alpha

- For detecting if the string is totally alpha numeric

But i have a problem in alpha and alpha numeric detection patterns. These two patterns must also accept spaces. How can i add the space to the patterns?

Thank you!
Question by:blacklord
  • 3
LVL 32

Expert Comment

ID: 13782500
\s is the special character for spaces. You could also use \W in preg_replace instead of [a-zA-Z0-9_]


$string = "alfabeta09394! ?alfa";
$string = preg_replace("/[\W\s]/s","",$string);
echo $string;


LVL 32

Expert Comment

ID: 13782532
Sorry - you should use \w instead of \W (lowercase intead of uppercase). I misread your question and thought you wanted to remove everything EXCEPT alphanumeric characters.

The corrected versions of the code should be:

$string = "alfabeta09394! ?alfa";
$string = preg_replace("/[\w\s]/s","",$string);
echo $string;


\w represents all alpha numeric characters, while \W represents all non-alphanumeric characters.

LVL 32

Accepted Solution

Batalf earned 700 total points
ID: 13782606
Sorry for all the posts. Have been a very long day. Didn't intend to send all these posts or tag the question:-)

You're trying to match the string against a pattern.

Then the pattern should be



^ = start of strong
[\w\s]+ = followed by any number of alphanumeric characters or spaces
$ = end of string.



// Example one:
$string = "alfabeta09394 ! ?alfa";
$pattern = "/^[\w\s]+$/s";
    echo "Alpha characters only<br>";    
    echo "String contains non alphanumeric characters<br>";

// Example two:
$string = "alfabeta09394alfa";
    echo "Alpha characters and/or spaces only";    
    echo "String contains non alphanumeric characters";

LVL 25

Assisted Solution

by:Marcus Bointon
Marcus Bointon earned 300 total points
ID: 13783157
Using a regex to spot purely numeric values is a bit wasteful - you can just use is_numeric(): http://www.php.net/manual/en/function.is-numeric.php Otherwise Batalf has it taped.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses
Course of the Month15 days, 8 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question