PHP-如何避免替换字符串

PHP-如何避免替换字符串,php,string,replace,Php,String,Replace,我正在编写一个脚本,允许学生将答案输入到表格中,并对答案给予即时反馈 我从一个字符串($content)开始,该字符串包含完整的任务,空格在方括号中,如下所示: 房间里有人。房间里没有[任何人]。 房间里有人吗? 现在,脚本识别解决方案(某人、任何人、任何人),并将它们保存在一个数组中。学生的答案也在一个数组中 要查看答案是否正确,脚本将检查$input[$i]和$solution[$i]是否相同 现在问题来了:我希望脚本用一个输入框来替换占位符,其中的解决方案是错误的,而绿色的解决方案则是正确

我正在编写一个脚本,允许学生将答案输入到表格中,并对答案给予即时反馈

我从一个字符串($content)开始,该字符串包含完整的任务,空格在方括号中,如下所示:

房间里有人。房间里没有[任何人]。
房间里有人吗?

现在,脚本识别解决方案(某人、任何人、任何人),并将它们保存在一个数组中。学生的答案也在一个数组中

要查看答案是否正确,脚本将检查$input[$i]和$solution[$i]是否相同

现在问题来了:我希望脚本用一个输入框来替换占位符,其中的解决方案是错误的,而绿色的解决方案则是正确的。更新后的$content版本将显示在下一页。 但是,如果有两个相同的解决方案,这将导致多个替换,因为替换被再次替换

我尝试用限制为1的preg_replace,但这也不起作用,因为它不会跳过已被替换的解决方案

$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 $i=0; 而($solution[$i]){ //回答正确 如果($solution[$i]==$input[$i]){ //替换占位符>绿色解决方案 $content=str_replace($solution[$i],$solution_green[$i],$content); } //答错 否则{ //请替换占位符>输入框以重试 $content=str\u replace($solution[$i],$solution\u box[$i],$content); } $i++; } 打印$content//根据学生的答案输出新表格 有没有办法避免更换


我希望我没有太多闲逛。。。多年来,我一直在为这个问题绞尽脑汁,如果有任何建议,我将不胜感激。

我处理这个问题的方法是将原始内容分成与文本中的标记相关的部分。因此,如果您通过
]
将原始文本分解(),您将得到

Array
(
    [0] => There's [somebody
    [1] =>  in the room. There isn't [anybody
    [2] =>  in the room.
Is [anybody
    [3] =>  in the room?
)
如您所见,每个数组元素现在都与答案/解决方案编号相对应。因此,当替换文本时,它会改为
$parts[$i]
。同样作为一种保护措施,它取代了
[text
,以确保还有其他解决方案,但这应该可以完成这项工作

最后,代码通过使用
内爆()
并使用
]
将原始内容添加回来,从而重建原始内容

$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);

我采用的方法是将原始内容分割成与文本中的标记相关的部分。因此,如果您通过
]
将原始文本分解(),您将得到

Array
(
    [0] => There's [somebody
    [1] =>  in the room. There isn't [anybody
    [2] =>  in the room.
Is [anybody
    [3] =>  in the room?
)
如您所见,每个数组元素现在都与答案/解决方案编号相对应。因此,当替换文本时,它会改为
$parts[$i]
。同样作为一种保护措施,它取代了
[text
,以确保还有其他解决方案,但这应该可以完成这项工作

最后,代码通过使用
内爆()
并使用
]
将原始内容添加回来,从而重建原始内容

$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);
您可以使用
sprintf()
/
vsrpintf()
函数替换位置占位符,但首先必须为其准备句型。每个“解决方案占位符”都应替换为
%s
,以便以后
sprintf()
可以用相应的字符串替换每个占位符

您可以在循环中执行此操作:

$fields = [];
while (isset($solution[$i])) {
    $fields[] = ($solution[$i] === $input[$i])
        ? $solution_green[$i]
        : $solution_box[$i];

    //doesn't matter if you replace more than one here
    $content = str_replace($solution[$i], '%s', $content);
    $i++;
}

print vsprintf($content, $fields);
//or for php>=5.6: sprintf($content, ...$fields);
这是一个针对代码当前状态的简单解决方案。它可能会被重构(在解析正确的单词时进行模式替换,绿色/长方体数组可能会被生成所需字符串的方法替换…等等)

您可以使用
sprintf()
/
vsrpintf()
函数替换位置占位符,但首先您必须为它准备句型。每个“解决方案占位符”都应替换为
%s
,以便以后
sprintf()
可以用相应的字符串替换每个占位符

您可以在循环中执行此操作:

$fields = [];
while (isset($solution[$i])) {
    $fields[] = ($solution[$i] === $input[$i])
        ? $solution_green[$i]
        : $solution_box[$i];

    //doesn't matter if you replace more than one here
    $content = str_replace($solution[$i], '%s', $content);
    $i++;
}

print vsprintf($content, $fields);
//or for php>=5.6: sprintf($content, ...$fields);

这是一个针对代码当前状态的简单解决方案。它可能会被重构(在解析正确的单词时进行模式替换,绿色/长方体数组可能会被生成所需字符串的方法替换……等等)

谢谢您的建议!作为一个相对的初学者,我还不熟悉sprintf和vsprintf。我会好好读的。谢谢你的建议!作为一个相对的初学者,我还不熟悉sprintf和vsprintf。我会好好读一读的。我真的很感谢你抽出时间来帮我!工作起来很有魅力。:)我真的很感谢你抽出时间来帮助我!工作起来很有魅力。:)