我有这么简单的代码:
private function edit_keywords($text)
{
$tresc = str_replace("\n","",$text);
$tresc = str_replace("\r","",$text);
if(strpos($text,'"')!==FALSE)
{
array_push($this->warnings,"Not allowed character found in keywords \".");
return;
}
目前这会阻止输入“我想阻止'。如何做到这一点?
替换'也没关系。
答案 0 :(得分:0)
试试这个:
<?php
private function edit_keywords($text)
{
$tresc = str_replace("\n","",$text);
$tresc = str_replace("\r","",$text);
if ((strpos($text,'"')!==FALSE)||(strpos($text,"'")!==FALSE))
{
array_push($this->warnings,"Not allowed character found in keywords \".");
return;
}
?>
答案 1 :(得分:-1)
您可以使用双引号(strpos
)或通过转义单引号"'"
为单引号编写第二个'\''
支票。
if(strpos($text,'\'')!==FALSE)
{
array_push($this->warnings,"Not allowed character found in keywords \".");
return;
}
最终,您可能会达到要在数组中存储要测试的所有字符的复杂程度,并迭代它们,而不是不断添加新的if
语句:
$bad_chars = array('"', '\'');
foreach ($bad_chars as $bad_char) {
if (strpos($text, $bad_char) !== false) {
array_push($this->warnings,"Not allowed character found in keywords \".");
return; # or break, to stop after the first disallowed characte
}
}