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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?