PHP:  Strip everything but numbers and letters

Posted on 2012-09-19
Last Modified: 2012-09-19
Hi All,

How can I strip everything from a variable that isnt a number or letter?

Question by:detox1978
    LVL 74

    Accepted Solution

    You could use preg_replace:

    $result = preg_replace('/[^a-zA-Z0-9]/', '', $input);

    Open in new window

    ...where "[^a-zA-Z0-9]" means any character NOT ( [^...] ) a letter ( a-zA-Z ) or number ( 0-9 ).
    LVL 2

    Author Closing Comment

    awesome thanks!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    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 …
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to count occurrences of each item in an array.

    759 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

    10 Experts available now in Live!

    Get 1:1 Help Now