Sub-String Deletion

Discussion in 'PHP' started by TextAdMarket, May 6, 2008.

  1. #1
    I'm not entirely sure how to do this, but maybe someone here does.

    I have a string like this:
    $string = "index.php?id=5&value=7&page=4&customer=1";

    I want to remove the substring "page=4". The thing is, the page=4 value isn't guaranteed to be in that place, nor is it guaranteed to equal 4 (may equal multiple digits).

    I'm terrible with strings. Has anyone done something like this before?

    Thanks.
     
    TextAdMarket, May 6, 2008 IP
  2. french-webbie

    french-webbie Peon

    Messages:
    194
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hm,
    I would do it by exploding the string on the question mark and the ampersand, and then reconstruct it without the page part.
     
    french-webbie, May 6, 2008 IP
  3. Perow

    Perow Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    If you are sure the substring will always start with "page=", you could use regular expressions to take it out.
     
    Perow, May 7, 2008 IP
  4. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #4
    Using regex (strips out the leading "&" char too, if exists):

    $string = preg_replace('/&?page=\d+/si','',$string);
    PHP:
     
    wmtips, May 7, 2008 IP
  5. TextAdMarket

    TextAdMarket Peon

    Messages:
    520
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks! I ended up doing something just like that!
     
    TextAdMarket, May 7, 2008 IP