gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 23:31:41 25 4
gpt4 key购买 nike

我有以下场景:

我正在存储可能包含空格的用户输入。问题是,当我从数据库 (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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com