douze2890241475 2017-07-25 06:10
浏览 90
已采纳

如何在数据库中搜索特殊字符?

I have a search where visitors can search for an article's title. It's working great until they try to search for an utf-8 character.

Here's the problem: You search for "új" and you get results from title which contains "új", but not "Új".

In the database I have the uft-8 characters stored as html entities, so "új" looks like: "&#250 ;j" and "Új" looks like "&#218 ;".

I have a function, which converts these utf8 characters to html entities. E.g.:

$text = str_replace("Ú","Ú",$text);
$text = str_replace("ú","ú",$text);

What I want to do is, if someone searches for ú also show results for Ú, but because they are different characters in the database (&#218 ; and &#250 ;), therefore strtolower (or mb_strtolower) doesn't work, it only works for normal characters like E and e or U and u.

What should I do? Would it be better to store the utf-8 characters as utf-8 in the database?

  • 写回答

1条回答 默认 最新

  • douan6815 2017-07-25 06:20
    关注

    Since you want this data for search optimization purposes what I would do is:

    • Add a slug field to the database, that contains the tittle as a lowercase non UTF8.

    • When searching conver the user input query to the same slug using that library

    For more information you could take a look here you will need to write a migration script to convert all the already existing titles to slug strings and add that colum to the database, but that should be more or less trivial.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?