1

我有以下情况:

我正在存储可能包含空格的用户输入。问题是,当我SELECT从数据库 ( MySql) 中执行操作时,它找不到匹配项。如果我去掉空格,则在 db 上的搜索有效,但是如果我以这种方式存储字符串,这些字符串看起来都很乱。

我怎样才能正确地在数据库中存储字符串(带有空格)AND正确地执行SELECT将找到那些存储的字符串的语句?

谢谢您的帮助。

更新:

这是我现在正在使用的内容:

$filename = "这是一个带空格的字符串";
//像这样存储在数据库中

$trim_string = preg_replace("/\s+/", "", $filename);
//搜索数据库为Thisisastringwithwhitespace

仍然找不到匹配项?

4

1 回答 1

0

If the user would know the entire string, I would go with Blake suggestion of using TRIM() in the Mysql query, if you want the partial search, and want it fast, I would create a secondary table with the words and another one for the position where appears in the strings, something like this :

Word( id, string )
WordInString( id, source_string_id, word_id, position )

The search would be in 2 steps :

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.

Hope it helps !

于 2013-02-12T12:28:39.800 回答