1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Need PHP help

Discussion in 'PHP & Perl' started by DaBomb, Jul 28, 2009.

  1. DaBomb

    DaBomb Newbie

    Joined:
    Jul 22, 2009
    Messages:
    5
    Likes Received:
    0
    having a difficult time with this.. wondering if anyone here can help.

    Let's say I have the following URL string:
    Code:
    http://www.google.com/search?hl=en&blah=0123456789&safe=off&client=firefox-a&rls=org.mozilla%3Aen-US%3Aofficial&hs=cf
    
    
    I need to replace the value of blah, blah is always 10 characters. the position of blah can change within the URL string.


    Thanks in advance!
     
  2. geezer101

    geezer101 Junior Member

    Joined:
    Mar 15, 2009
    Messages:
    139
    Likes Received:
    121
    Location:
    UK
  3. DaBomb

    DaBomb Newbie

    Joined:
    Jul 22, 2009
    Messages:
    5
    Likes Received:
    0
    I'm familiar with that page and php string concepts.. just having a difficult time with this
     
  4. justin

    justin Jr. VIP Jr. VIP Premium Member

    Joined:
    Oct 15, 2007
    Messages:
    505
    Likes Received:
    392
    Code:
    <?php
    echo str_replace("blah=","replacedvalue","yourURL");
    ?> 
    try this?
     
  5. DaBomb

    DaBomb Newbie

    Joined:
    Jul 22, 2009
    Messages:
    5
    Likes Received:
    0

    That won't work... it would only replace blah=, and the not the value of it.


    this worked:


    Code:
    
                        $text = "http://www.google.com/search?hl=en&blah=0123456789&safe=off&client=firefox-a&rls=org.mozilla%3Aen-US%3Aofficial&hs=cf";
                        $InjectedValue = "999999999";
                        
                        $StartPos = strripos($text,"blah="); 
                        $textFront = substr($text, 0, $StartPos+5);
                        $textEnd = substr($text, $StartPos+15);
                        $text = $textFront . $InjectedValue . $textEnd;
    
    
     
  6. cyklotrial

    cyklotrial Regular Member

    Joined:
    Oct 13, 2008
    Messages:
    248
    Likes Received:
    82
    Location:
    Wonderland
    simple regex :)
    PHP:
    <?php
    $string 
    'http://www.google.com/search?hl=en&blah=0123456789&safe=off&client=firefox-a&rls=org.mozilla%3Aen-US%3Aofficial&hs=cf';
    $pattern '/blah=........../i';
    $replacement 'blah=I_LOVE_BHW';
    echo 
    preg_replace($pattern$replacement$string);
    ?>
    regards
    Lukas
     
  7. DaBomb

    DaBomb Newbie

    Joined:
    Jul 22, 2009
    Messages:
    5
    Likes Received:
    0

    that's exactly what I was looking for, but my regex sucks, so I took the easy way out.


    what regex can I use that will match blah=9999999999 (i.e. blah= followed by 10 integers)
     
  8. orangejuice

    orangejuice Registered Member

    Joined:
    Jul 25, 2009
    Messages:
    54
    Likes Received:
    16
    You can use this if you want to go the regex way:

    PHP:
    <?php
    $url 
    'http://www.google.com/search?hl=en&blah=0123456789&safe=off&client=firefox-a&rls=org.mozilla%3Aen-US%3Aofficial&hs=cf';
    $key 'blah';
    $replace 'HelloWorld';
    echo 
    preg_replace("#{$key}=[^&]+&#""{$key}={$replace}&"$url);
    ?>
    That will replace everything from the = right up to the &.
     
  9. fukinlee

    fukinlee Newbie

    Joined:
    Aug 11, 2009
    Messages:
    22
    Likes Received:
    3
    Simple:

    Code:
    $replacement_value = 'ILikeBoobs';
    $query = 'http://www.google.com/search?hl=en&blah=0123456789&safe=off&client=firefox-a&rls=org.mozilla%3Aen-US%3Aofficial&hs=cf';
    $new_query = preg_replace('/blah=.+&/', 'blah='.$replacement_value.'&', $query);
    //$new_query will be http://www.google.com/search?hl=en&blah=ILikeBoobs&safe=off&client=firefox-a&rls=org.mozilla%3Aen-US%3Aofficial&hs=cf
    
     
  10. 00CivicEX

    00CivicEX Jr. VIP Jr. VIP Premium Member

    Joined:
    Mar 3, 2009
    Messages:
    293
    Likes Received:
    214
    i think your pregreplace needs to be
    PHP:
    /blah=.+[0-9]
    Cause your's will match everything from the .+ The one I posted will only grab the numbers