SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to strip anything except for numbers, letters and spaces?

    Can someone tell me how to remove anything from a variable except for letters, numbers, or spaces? For example, if it has & or dashes or anything else like that i don't want it to remain in the string.
    John Saunders

  2. #2
    SitePoint Evangelist AlienDev's Avatar
    Join Date
    Feb 2007
    Location
    UK
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know this isnt what you asked for, but heres the regex for checking if the variable only has letters, numbers or spaces in it:

    PHP Code:
    if (preg_match("/[^a-zA-Z0-9 ]/",$var)) {
    echo 
    'You may only use letters, numbers and spaces!';
    } else {
    echo 
    'Heres a cookie';


  3. #3
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Or can change that to
    PHP Code:
    $data preg_replace('/[^a-z0-9 ]/i'''$data); 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



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
  •