str_replace wildcard

Discussion in 'PHP' started by crath, Jan 3, 2009.

  1. #1
    I have "random text bla bla bla andmore random text;"

    and I want to remove everything from "andmore" and after.

    I was hoping there was a way to wildcard in str_replace, so i could do something like

    str_replace("andmore".wildcard.";" , "" , $string);

    thanks to any help!
     
    crath, Jan 3, 2009 IP
  2. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #2
    I am just wondering why are you not using explode() function (just explode white spaces) or get a simple regular expression for that
    g'luck!
     
    crivion, Jan 3, 2009 IP
    crath likes this.
  3. crath

    crath Well-Known Member

    Messages:
    661
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    100
    #3
    gah why didn't i think of that :p

    thank you!
     
    crath, Jan 3, 2009 IP
  4. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #4
    Removes at first instance of andmore.
    <?php
    
    $string = "random text bla bla bla andmore random text;";
    
    $string = substr($string, 0, ($s = strpos($string, 'andmore'))!== false ? $s : strlen($string));
    
    echo $string;
    PHP:
     
    Danltn, Jan 3, 2009 IP
  5. crath

    crath Well-Known Member

    Messages:
    661
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    100
    #5
    thanks, I just ended up doing

    <?php
    $string = "random text bla bla bla andmore random text;";
    
    $stripped = explode("andmore",$string);
    
    echo $stripped[0];
    ?>
    PHP:
     
    crath, Jan 4, 2009 IP