Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.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_Mysql - Fatal编程技术网

PHP,数据库:用空格保存和搜索字符串

PHP,数据库:用空格保存和搜索字符串,php,mysql,Php,Mysql,我有以下情况: 我正在存储可能包含空格的用户输入。问题是,当我从数据库MySql中进行选择时,它没有找到匹配项。如果我去掉空白,db上的搜索就可以工作,但是如果我这样存储字符串,字符串看起来都很乱 如何在数据库中正确地存储带空格的字符串,并正确地执行SELECT语句来查找存储的字符串 谢谢你的帮助 更新: 以下是我现在的工作内容: $filename = "This is a string with white space"; //being stored in the db like this

我有以下情况:

我正在存储可能包含空格的用户输入。问题是,当我从数据库MySql中进行选择时,它没有找到匹配项。如果我去掉空白,db上的搜索就可以工作,但是如果我这样存储字符串,字符串看起来都很乱

如何在数据库中正确地存储带空格的字符串,并正确地执行SELECT语句来查找存储的字符串

谢谢你的帮助

更新:

以下是我现在的工作内容:

$filename = "This is a string with white space"; //being stored in the db like this $trim_string = preg_replace("/\s+/", "", $filename); //search db as Thisisastringwithwhitespace
但是仍然没有找到匹配项?

如果用户知道整个字符串,我会同意Blake的建议,在Mysql查询中使用TRIM,如果您希望部分搜索,并且希望搜索速度快,我会创建一个包含单词的辅助表,并为字符串中出现的位置创建另一个表,如下所示:

Word( id, string )
WordInString( id, source_string_id, word_id, position )
搜索将分为两个步骤:

1. get the words ids
2. get from WordInString the top list with greater number of Words hits, and show the options to the end user.

希望有帮助

存储未编辑的内容并按修剪版本搜索?聪明!:P另一个选择是使用正则表达式。所谓空白,是指尾随空格吗?请提供示例。主要是单词之间的空格,但可能有人输入带有尾随空格的字符串。请更新问题,因此存储不是问题?