Regual expressions for detecting numeric, alpha numeric and alpha strings

Posted on 2005-04-14
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
    LVL 32

    Expert Comment

    \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

    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

    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

    Using a regex to spot purely numeric values is a bit wasteful - you can just use is_numeric(): Otherwise Batalf has it taped.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Join & Write a Comment

    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    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.

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now