添加一个“+”来查询每个<空格> php


Add a '+' to query for each <space> php

>Html

<html>
<body>
    <h1>What do you want?</h1>
    <form action="googleApi.php" method="post" />
    <input type="text" name="itemWanted" /> <br />
    <input type="submit" name="submit" />
    </form>
</body>
</html>

.PHP

<?php
//Escape the input
$itemQueried = mysql_real_escape_string($_POST['itemWanted']);

我如何获取此代码以将+登录切换为值的任何空间 $_POST['itemWanted'] 所以现在,如果我输入"bag",它工作正常。但是如果我输入"Gucci Bag",它不会填充数组,因为查询(脚本(看起来像"Gucci Bag",而如果你使用空格,查询实际上应该是"Gucci+Bag"。如何将+切换为<space>

您可以使用

urlencode,这会将任何空格转换为+

例如

urlencode("Gucci Bag"); // Will give "Gucci+Bag"

因此,只需使用 urlencode($_POST['itemWanted']) 而不是 $_POST['itemWanted'],用户输入的任何空格都将转换为+,以便在您创建$url