PHP,数据库 : Preserving and searching strings with whitespace

标签 php mysql

我有以下场景:

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

如何在数据库中正确存储字符串(带空格)AND 正确执行 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.

希望对您有所帮助!

关于PHP,数据库 : Preserving and searching strings with whitespace,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10260906/

相关文章:

mysql - 如果行存在则更新,否则使用另一个表中的数据插入新记录

php - 调用mysql存储过程后执行查询

php - 加密电子邮件、散列密码并将其存储在数据库中?

PHP更改密码(旧密码与数据库内的旧密码不匹配)

php - 创建评论 anchor

php - 如何在数据库中插入波斯语链接?

mysql - 服务器重启后无法在 Docker 容器内连接到 MySQL

php - 无法增加静态类变量 PHP

php - 使用 Carbon 返回人类可读的日期时间差

php - Laravel - 如何从文章标题正确生成独特的 slug?