SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    United States
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regular Expressions, stripping everything but letters, spaces, and digits

    How do I go about removing everything in a string besides letters, spaces, and digits?

    So far I have

    $newvar = ereg_replace("[^A-Za-z0-9]", "", $string);

    That takes care of the letters and digits, but not the spaces.

    Help?

  2. #2
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    spaces:
    \s //any whitespace spaces, new lines tabs

    try that
    Need any video services done? PM me
    Watermarking, Thumbnails, Video Content Submission,
    Encoding videos including Flash (.flv).

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    United States
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have tried things like
    $newvar = ereg_replace("[^A-Za-z0-9\s]", "", $string);

    but that hasn't worked

  4. #4
    SitePoint Member
    Join Date
    Feb 2006
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try to use trim() to remove spaces

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That takes care of the letters and digits, but not the spaces.
    1) Why are you using ereg()? Everything I've read says you should use preg() and forget that ereg() even exists.

    2) Shortcuts for character classes don't work with ereg(), e.g. \s.

    3) A space is represented by? How about......a space!
    Code:
    $regex ="/[^0-9a-zA-Z ]/";
                         ^
    $str = "a-A&a 9-8,7*6 b B b";
    echo  preg_replace($regex, "", $str);


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
  •