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条)

报告相同问题?

悬赏问题

  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥30 数字信号处理实验报告
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥15 ensp路由器启动不了一直报#
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要