SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru
    Join Date
    Sep 2004
    Location
    Provo, UT
    Posts
    865
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    A function to strip out alpha characters?

    I have a field that requires only numbers on one of my forms. Is there a function that would strip out all letters and just leave the numbers?

    Thanks!
    Convert your dollars into silver coins. www.convert2silver.com

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    preg_replace would do it with an appropriate pattern.

    PHP Code:
    <?php
    preg_replace
    ('~[^0-9]~'null$string);
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Guru
    Join Date
    Sep 2004
    Location
    Provo, UT
    Posts
    865
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I tried that and it didn't work. I was still able to fill out the form with the following data:

    123 E. Main

    Shouldn't the code snip that you provided reduced the string to:

    123


    Thanks

    Rob
    Convert your dollars into silver coins. www.convert2silver.com

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,861
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    See http://au.php.net/manual/en/filter.filters.sanitize.php for what filters are available to sanitize input data in PHP and see http://au.php.net/manual/en/filter.e...nitization.php for an example of how to code it.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •