我需要“转义”字符串变量中的某些字符。我的方法是通过单个char将字符串拆分为数组,循环数组,如果char匹配特殊字符,则在其前面添加“\”。但是以下代码的某些部分是不正确的,它会导致页面崩溃。
$arrayInput = str_split($newInput);
$command = "";
foreach($arrayInput as $character) {
echo "$character";
if($character == "<"){
$character = "\<"
} else if ($character == ">") {
$character = "\>"
} else {
}
command += $character;
}