i want to remove every thing in my string before first -

Discussion in 'PHP' started by abdo629, Aug 23, 2011.

  1. #1
    i want to remove every thing in my string before the -

    $url = "jdhsfiusdif-keep this"

    i also want to remove - i want only to keep "keep this"

    want your help
     
    Solved! View solution.
    Last edited: Aug 23, 2011
    abdo629, Aug 23, 2011 IP
  2. [GotHost] James

    [GotHost] James Guest

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    $before = strstr($url, '-', true);
    
    PHP:
    If you don't have PHP5, but rather PHP4, use the following:

    
    $before = substr($url, 0, strpos($url, '-'));
    
    PHP:
     
    [GotHost] James, Aug 23, 2011 IP
  3. abdo629

    abdo629 Greenhorn

    Messages:
    63
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    thanks

    but i want also to remove -
     
    abdo629, Aug 23, 2011 IP
  4. #4
    Hy,
    You can use preg_replace():
    <?php
    $url = "jdhsfiusdif-keep this";
    $remove = '/^([^-]+-)/';
    $url2 = preg_replace($remove, '', $url);
    echo $url2;        // keep this
    ?>
    PHP:
     
    MarPlo, Aug 23, 2011 IP
  5. [GotHost] James

    [GotHost] James Guest

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Oh apologies, I read your post incorrectly.

    You want "keep this", in which case:
    
    $url = substr(strstr($url, '-'), 1);
    
    PHP:
    My original example keeps everything before the first '-'. This one keeps everything after the first '-'.
    As for MarPlo's example, it'll likely work fine but strstr is much more efficient, faster.
     
    [GotHost] James, Aug 23, 2011 IP
  6. abdo629

    abdo629 Greenhorn

    Messages:
    63
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #6
    thanks verrrrrrrry much for your help it really works
     
    abdo629, Aug 23, 2011 IP
  7. abdo629

    abdo629 Greenhorn

    Messages:
    63
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #7
    i want to thank you for your interest you are so kind

    your code works too

    thanks so much
     
    abdo629, Aug 23, 2011 IP
  8. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #8
    Just FYI, substr() is faster and less memory intensive than strstr().
     
    Rukbat, Aug 23, 2011 IP
  9. [GotHost] James

    [GotHost] James Guest

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    They don't do the same thing. substr returns a substring from a starting position to an ending position in a string. strstr returns the part of a string from the position of a given needle.
    Can't be compared like that really unless you were referring to my substr/strpos combination above, in which case I did use substr.
     
    [GotHost] James, Aug 23, 2011 IP
  10. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #10
    I was. I was just pointing out that doing it that way is better than using strstr().
     
    Rukbat, Aug 23, 2011 IP
  11. Technoslab

    Technoslab Peon

    Messages:
    46
    Likes Received:
    3
    Best Answers:
    3
    Trophy Points:
    0
    #11
    I'd do it like this.

    
    function keepthis($var){
      $str = explode("-",$var);
      return $str[1];
    }
    
    Code (markup):
     
    Technoslab, Aug 23, 2011 IP
  12. [GotHost] James

    [GotHost] James Guest

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Then you're left with the problem which occurs when $var = 'abc-def-ghi'. It'd return 'def' rather than 'def-ghi'.
    In this case it'd work fine though.
     
    [GotHost] James, Aug 24, 2011 IP
  13. kanikakaminial

    kanikakaminial Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    <?php
    echo substr("Str");
    ?>
     
    kanikakaminial, Aug 25, 2011 IP