str_replace how to

Discussion in 'PHP' started by redbayron2006, Sep 11, 2008.

  1. #1
    Well i have a list in a txt with some 100ds rows containg a number with 3 digits and a word. I want to remove the number from the list. So i create a script php and i inport all the rows in an array and then i go with str_replace to change the array but it doesnt work can anyone helpme with this?

    $keyword[$i] = "121 keyword1"
    $keyword[$i] = str_replace("[0-9]+", " ", $keyword[$i]);

    I want to take only $keyword[$i] = "keyword1"

    Can anyone helpme with this?
     
    redbayron2006, Sep 11, 2008 IP
  2. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #2
    jayshah, Sep 11, 2008 IP
  3. m0nkeymafia

    m0nkeymafia Well-Known Member

    Messages:
    399
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    125
    #3
    Untested but should probably work
    
    $keyword[$i] = ereg_replace("[0-9]+", " ", $keyword[$i]);
    
    Code (markup):
     
    m0nkeymafia, Sep 11, 2008 IP
  4. serialCoder

    serialCoder Guest

    Best Answers:
    0
    #4
    you can this using this pseudocode

    get the list and convert them into an array
    loop through the list, us regex to take out the numbers
    rewrite the results into a new file
     
    serialCoder, Sep 11, 2008 IP
  5. redbayron2006

    redbayron2006 Active Member

    Messages:
    173
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #5
    I did that it works but its a little bit noob
    Any suggestions?

    $keyword[$i] = str_replace(array("1", "2", "3", "4", "5", "6", "7", "8", "9", "0"), "", $keyword[$i]);
    $keyword[$i] = ucfirst(trim($keyword[$i]));
     
    redbayron2006, Sep 11, 2008 IP
  6. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #6
    Well, if you want to go that way ...

    $keyword[$i] = str_replace(range(0, 9), "", $keyword[$i]);
    $keyword[$i] = ucfirst(trim($keyword[$i]));
    PHP:
    ... is an easier implementation (but the same).

    Jay
     
    jayshah, Sep 11, 2008 IP
  7. redbayron2006

    redbayron2006 Active Member

    Messages:
    173
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #7
    Thanks jayshah thats cool!
     
    redbayron2006, Sep 11, 2008 IP