Need PHP help

DaBomb

Newbie
Joined
Jul 22, 2009
Messages
5
Reaction score
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!
 
I'm familiar with that page and php string concepts.. just having a difficult time with this
 
Code:
<?php
echo str_replace("blah=","replacedvalue","yourURL");
?>

try this?
 
Code:
<?php
echo str_replace("blah=","replacedvalue","yourURL");
?>
try this?


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;
 
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
 
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


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)
 
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 &.
 
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
 
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
 
Back
Top