Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to know how to replace some text in php string to different values using preg_replace.

For example :

I want this result :

I have used this code :

$string = 'http://www.mysite.com/?uid=123456<br/>
            http://www.mysite.com/?uid=123456<br/>
            http://www.mysite.com/?uid=123456';

$string = preg_replace(array("~123456~", "~123456~", "~123456~"), array("456789","789456","159753"), $string);

echo $string;

but it gives me this result :

Any help ?

Thanks !

share|improve this question
 
How are you getting the uid numbers in the result you want? They look random? –  Phil Cross May 30 at 15:43
add comment

2 Answers

up vote 0 down vote accepted

Try:

$string = 'http://www.mysite.com/?uid=123456<br/>
        http://www.mysite.com/?uid=123456<br/>
        http://www.mysite.com/?uid=123456';

$newString = preg_replace_callback('/([0-9]{6})/im', function($matches){ return rand(100000, 999999); }, $string);
share|improve this answer
 
It works, thank you :) –  aimadnet May 30 at 15:53
 
@aimadnet , Upvote or accept answer? :) –  Phil Cross May 30 at 16:04
add comment

you can test "~http://www\.mysite\.com/\?uid\=([0-9]+)~si" it find all with different uid value and you can use preg_replace_callback for check and replace them

share|improve this answer
 
Thanks for your help, it works :) –  aimadnet May 30 at 15:54
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.