Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
299 views
in Technique[技术] by (71.8m points)

php - Replace multiple regex matches with different strings

I'm trying to replace multiple regex matches with different text, just like that, but not working :/

This is my string:

str = [answers] => [{"pref1":0,"answerTime":800},{"pref2":0,"answerTime":800},{"pref3":0,"answerTime":800},{"pref4":0,"answerTime":800},{"pref5":0,"answerTime":800}]

I want to replace each answer time do a random time from an array list, like:

$time = array('"answerTime":1000,"','"answerTime":800,"','"answerTime":1200,"','"answerTime":1100,"','"answerTime":1500,"','"answerTime":900,"');
$rnd1 = $time[array_rand($time, 1)];
$rnd2 = $time[array_rand($time, 1)];
$rnd3 = $time[array_rand($time, 1)];
$rnd4 = $time[array_rand($time, 1)];
$rnd5 = $time[array_rand($time, 1)];
$replace = array($rnd1, $rnd2, $rnd3, $rnd4, $rnd5);

But everytime i the method below, all the results return me the same time:

$postpop = preg_replace('#"answerTime":(.*?),"#', array($replace), $reqpop);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Judging by your other SO account's question using my previous coding attempt with your sample $_POST data, this should be right for your task:

Method #1: (The non-regex / more stable way)

$_POST=[
    'authToken'=>'0a65e943412453ecec35c814',
    'sessionId'=>'431503466924',
    'answers' => '[{"Boost":false,"answerTime":1300,"id":3},{"Boost":false,"answerTime":800,"id":1},{"Boost":false,"answerTime":900,"id":3},{"Boost":false,"answerTime":1000,"id":1},{"Boost":false,"answerTime":1200,"id":1}]',
    'userId' =>'2235'
];

$time=[800,900,1000,1100,1200,1500];
$answers=json_decode($_POST['answers'],true);  // convert "answers" value to an array
foreach($answers as &$a){                      // iterate each subarray
    $a['answerTime']=$time[array_rand($time)]; // replace the previous answerTime value with a new random one
}
$_POST['answers']=json_encode($answers);       // apply updated & re-encoded "answers" string to $_POST
var_export($_POST);

Method #2: (The regex / less stable way)

$_POST=[
    'authToken'=>'0a65e943412453ecec35c814',
    'sessionId'=>'431503466924',
    'answers' => '[{"Boost":false,"answerTime":1300,"id":3},{"Boost":false,"answerTime":800,"id":1},{"Boost":false,"answerTime":900,"id":3},{"Boost":false,"answerTime":1000,"id":1},{"Boost":false,"answerTime":1200,"id":1}]',
    'userId' =>'2235'
];

$time=[800,900,1000,1100,1200,1500];
$_POST['answers']=preg_replace_callback('/answerTime":Kd+/',function($m)use($time){return $time[array_rand($time)];},$_POST['answers']);
var_export($_POST);

The K in the regex pattern says: "start the fullstring match from here", then it matches only the digits that follow answerTime:.

Possible output with either method:

array (
  'authToken' => '0a65e943412453ecec35c814',
  'sessionId' => '431503466924',
  'answers' => '[{"Boost":false,"answerTime":1200,"id":3},{"Boost":false,"answerTime":1000,"id":1},{"Boost":false,"answerTime":1500,"id":3},{"Boost":false,"answerTime":900,"id":1},{"Boost":false,"answerTime":800,"id":1}]',
  'userId' => '2235',
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...