-1

我得到了这样简单的代码:

    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;
    }

目前这将阻止进入我也想阻止。如何做到这一点?

替换 ' 也可以。

4

2 回答 2

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;
    }

?>
于 2012-04-25T13:17:07.027 回答
-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
  }
}
于 2012-04-25T13:16:45.847 回答