SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,207
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    removing all numbers

    Code:
    code
    
    $myVar='xy12 d2_6 dkr';
    
    $myvar=$str_replace('0','', $myVar);
    $myvar=$str_replace('1','', $myVar);
    $myvar=$str_replace('2','', $myVar);
    $myvar=$str_replace('3','', $myVar);
    $myvar=$str_replace('4','', $myVar);
    $myvar=$str_replace('5','', $myVar);
    $myvar=$str_replace('6','', $myVar);
    $myvar=$str_replace('7','', $myVar);
    $myvar=$str_replace('8','', $myVar);
    $myvar=$str_replace('9','', $myVar);
    
    echo $myVar;
    
    result
    
    xy d_ dkr
    The code above removes all numbers of the variable '$myVar',
    I guess there may be better code for it.
    Do you have any?
    Maybe regular expression...

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,807
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Yep, a regular expression would be easier
    PHP Code:
    $myVar preg_replace('/[0-9]+/'''$myVar); 
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,595
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Or slightly shorter:

    $myVar
    = preg_replace('/\d+/', '', $myVar);
    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="^$">

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,807
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    Or slightly shorter:

    $myVar
    = preg_replace('/\d+/', '', $myVar);
    For some reason I thought \d would remove + and - signs so I didn't use it, but I guess it doesn't, so yes, that is a good alternative too.
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes


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
  •