我有一个来自URL的$ _GET [q]。我正在尝试将搜索词回到搜索框中。有时人们可能会提交封装在引号中的查询,在这些情况下,ECHO会解释搜索词
ECHO $_GET[q];
如:
ECHO ""search term"";
结果我得到一个空白的搜索框。使用单引号搜索查询,例如:Peter的房子,工作正常。
我用的时候:
mysqli_real_escape_string($conn, $_GET[q])
我只在搜索框中得到一个反斜杠。
如何使用双引号封装的搜索词填充搜索框?
你也可以使用:
echo str_replace('"','',$_GET[q]);
这当然会删除所有双引号,所以如果它们在搜索词的某个地方可能有效,那么最好可能是:
echo str_replace('"','"',$_GET[q]);
没有测试过,但这可能也有效:
echo html_entity_decode(htmlentities($_GET[q))
你可以试试:
$str = "Hello World!";
echo $str . "<br>";
echo chop($str,"");
输出:
Hello World
说明:chop()
函数,帮助您切断任何人可能添加到字符串的引号。您可以根据代码进行操作。
$str = preg_replace( '["|\']','', $_REQUEST['q'] );
echo( $str ); //no double, no single quotes, faster than str_replace when you have to make more than 1 call to str_replace
要么...
$str = str_replace( '"','', $_REQUEST['q'] ); //no double quotes, faster than preg_replace when you only make one call to str_replace
echo( $str ); //no double quotes