douxi3404 2019-05-11 17:45
浏览 94
已采纳

PHP - 如何避免替换替换字符串

I'm working on a script that allows students to input their answers into a form and gives them instant feedback on their answers.

I start with a string ($content) that contains the complete task with the gaps in square brackets, something like this:

There's [somebody] in the room. There isn't [anybody] in the room. Is [anybody] in the room?

Now the script recognizes the solutions (somebody, anybody, anybody) and saves them in an array. The student's answers are also in an array.

To see if the answer is correct, the script checks if $input[$i] and $solution[$i] are identical.

Now here is the problem: I want the script to replace the placeholders with an input box where the solution is wrong and the solution in green where it's correct. This updated version of $content is then shown on the next page. But if there are two identical solutions, this results in multiple replacements as the replacement is replaced again...

I tried preg_replace with a limit of 1 but this doesn't do the trick either as it doesn't skip solutions that have already been replaced.


$i=0;

while ($solution[$i]){

//answer correct
    if($solution[$i] == $input[$i]){ 

        //replace placeholder > green solution     
            $content = str_replace($solution[$i], $solution_green[$i], $content);  
    }

//answer wrong
    else{ 

        //replace placeholder > input box to try again  
             $content = str_replace($solution[$i], $solution_box[$i], $content);  

    }
    $i++;
}

print $content;  //Output new form based on student's answer

Is there any way to avoid replacing replacements?

I hope I didn't ramble too much... Have been wracking my brain for ages over this problem and would appreciate any suggestions.

  • 写回答

2条回答 默认 最新

  • douyan2970 2019-05-11 18:16
    关注

    The way I've approached this is to split the original content into segments which relate to the markers in the text. So then you explode() the original text by ], you end up with...

    Array
    (
        [0] => There's [somebody
        [1] =>  in the room. There isn't [anybody
        [2] =>  in the room.
    Is [anybody
        [3] =>  in the room?
    )
    

    As you can see, each array element now corresponds with the answer/solution numbers. So when replacing the text, it changes $parts[$i] instead. Also as a safeguard, it replaces [text to make sure, there are other solutions, but this should do the job.

    At the end, the code then rebuilds the original content by using implode() and using ] to add it back.

    $parts = explode("]", $content);
    $i=0;
    
    while (isset($solution[$i])){
        //answer correct
        if($solution[$i] == $input[$i]){
            //replace placeholder > green solution
            $parts[$i] = str_replace("[".$solution[$i], "[".$solution_green[$i], $parts[$i]);
        }
        //answer wrong
        else{
            //replace placeholder > input box to try again
            $parts[$i] = str_replace("[".$solution[$i], "[".$solution_box[$i], $parts[$i]);
        }
        $i++;
    }
    $content = implode( "]", $parts);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档