Forum Moderators: coopster

Message Too Old, No Replies

str replace random numeric

random numeric str_replace

         

frogz

12:14 am on Jan 5, 2009 (gmt 0)

10+ Year Member



any idea how I can:

return str_replace('apples-<$RANDOM NUMERIC INPUT(1)HERE$>' , 'oranges-<$SAME NUMBER AS RANDOM INPUT(1)$>' , $s )

I cant figure out how to expect a random number, replace oranges with apples retaining the random number (var)

any light on this would be appreciated!
thnx in advance

eelixduppy

1:13 am on Jan 5, 2009 (gmt 0)



Depending on the format of the string the pattern might be slightly different, however, this is how I would go about doing it:

$string = 'apples-1777732'; # test case
#
$pattern = '/apples-(\d+)/';
$replacement = 'oranges-\\1'; # \\1 here is the random number
$new_string = [url=http://www.php.net/preg-replace]preg_replace[/url]($pattern, $replacement, $string);
echo $new_string;

frogz

1:35 am on Jan 5, 2009 (gmt 0)

10+ Year Member



ty elix !