preg_replace help neede.

Discussion in 'PHP' started by Jeehan, Aug 18, 2010.

  1. #1
    I tried this code:
    _______________________________________________________
    $userid= "xxx-username23"

    $correctname = preg_replace("/[^a-zA-Z]/", "", $userid);
    $correctname2 = preg_replace('[xxx]', "", $correctname);

    echo $correctname2 ;

    result showing "username"
    _______________________________________________________

    But i need the result = "username23",

    i am trying to remove only "xxx-" from $userid

    Please help.
     
    Jeehan, Aug 18, 2010 IP
  2. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #2
    As long as its xxx- this is all you need.

    
    $userid = "xxx-username23";
    
    $correctname = preg_replace('#xxx-#', '', $userid); //replaces xxx- with nothing
    
    echo $correctname;
    
    //result showing "username23"
    
    PHP:
     
    MyVodaFone, Aug 18, 2010 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    EDIT:

    A second too late...

    
    $foo = preg_replace('/^x{3}-/', '', $userid);
    
    PHP:
     
    nico_swd, Aug 18, 2010 IP
    Jeehan likes this.
  4. Jeehan

    Jeehan Well-Known Member

    Messages:
    1,578
    Likes Received:
    31
    Best Answers:
    1
    Trophy Points:
    115
    #4
    trying, thank you guys
     
    Jeehan, Aug 18, 2010 IP
  5. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #5
    Or as long as the the username is separated with a -

    This will do:
    $userid = "12Ab-username23";
    
    $correctname = preg_replace('#(.*-)#', '', $userid); //replaces xxx- with nothing
    
    echo $correctname;
    PHP:
     
    MyVodaFone, Aug 18, 2010 IP
    Jeehan likes this.
  6. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #6
    If that's the only dash that can be in the user name you have lots of options:

    list($dummy, $correctname) = explode('-', $userid);
    echo $correctname;
    PHP:
    if the prefix you want to remove is always three chars long, just chop the string off:
    $correctname = substr($userid, 4);
    echo $correctname;
    PHP:
     
    exam, Aug 18, 2010 IP