Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 由于搜索输入中有空格,提交搜索不起作用_Php_Regex - Fatal编程技术网

Php 由于搜索输入中有空格,提交搜索不起作用

Php 由于搜索输入中有空格,提交搜索不起作用,php,regex,Php,Regex,当我搜索只有一个单词的东西时,它会显示结果,但当我搜索有两个单词的东西时,它不会显示结果。可能是因为他们之间的空间…有什么解决办法吗 This is the code which I am using: <?php include("config.php"); include("session-user.php"); if(isset($_POST['search']) && $_POST['search'] != "") { $searchq = $_POST['sea

当我搜索只有一个单词的东西时,它会显示结果,但当我搜索有两个单词的东西时,它不会显示结果。可能是因为他们之间的空间…有什么解决办法吗

This is the code which I am using:

<?php include("config.php");
include("session-user.php");
if(isset($_POST['search']) && $_POST['search'] != "") {
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);

$query = "SELECT * FROM tbl_product WHERE product_title LIKE '%$searchq%' and is_delete = '0'";
$sql=mysqli_query($con,$query);
$count = mysqli_num_rows($sql);
while($row = mysqli_fetch_array($sql)){
    $product_title = $row['product_title'];
    $id = $row['product_id'];}?>
<script>
    window.location="product.php?pid=<?php echo $id?>";
</script>
<?php
}
?>

<div class="ui-widget">
<input id="automplete-3" type="text" class="form-control" placeholder="Search" name="search">
</div>
这是我正在使用的代码:
window.location=“product.php?pid=”;
在(“#[^0-9a-z]#i”之间留出空格

$searchq = preg_replace("#[^0-9a-z]#i","",$searchq); 
将您的空间隐藏为空白

例如

$searchq="one two";//output onetwo
$searchq="one two";//output one two
在正则表达式中添加空格作为

echo $searchq = preg_replace("#[^0-9a-z\s]#i","",$searchq);
                                       ^^^^
它将产生

例如

$searchq="one two";//output onetwo
$searchq="one two";//output one two
使用trim()例如
$searchq=trim($_POST['search']);
它将删除不在两个字符或单词之间的尾随空格。
$searchq=preg_replace(“#[^0-9a-z]#i”,“,$searchq)
这将您的空格转换为空白