PHP 中包含双引号的回显文本
Echo text including double quotes in PHP
我有一个来自 URL 的 $_GET[q]。我正在尝试将搜索词回显到搜索框中。有时人们可能会提交包含在引号中的查询,在这些情况下,ECHO 会解释搜索词
ECHO $_GET[q];
如:
ECHO ""search term"";
结果我得到一个空白的搜索框。使用单引号搜索查询,例如:Peter's house, work fine。
当我使用:
mysqli_real_escape_string($conn, $_GET[q])
我在搜索框中只看到一个反斜杠。
如何使用用双引号括起来的搜索词填充搜索框?
你可以使用 addslashes
像这样:
$t = 'peter "pan"';
echo addslashes($t); // outputs: peter \"pan\"
您还可以使用:
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
我有一个来自 URL 的 $_GET[q]。我正在尝试将搜索词回显到搜索框中。有时人们可能会提交包含在引号中的查询,在这些情况下,ECHO 会解释搜索词
ECHO $_GET[q];
如:
ECHO ""search term"";
结果我得到一个空白的搜索框。使用单引号搜索查询,例如:Peter's house, work fine。
当我使用:
mysqli_real_escape_string($conn, $_GET[q])
我在搜索框中只看到一个反斜杠。
如何使用用双引号括起来的搜索词填充搜索框?
你可以使用 addslashes
像这样:
$t = 'peter "pan"';
echo addslashes($t); // outputs: peter \"pan\"
您还可以使用:
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