我有包含以下信息的 SQL 行: 示例 - Exampleish (Exampleishbosh)
并不是所有的都有 (),所以最好去掉
- and ()
从 url 中取出,然后使用 SQL 查询中的剩余单词进行搜索?
http://www.co.com/example_exampleish_exampleishbosh/
或者这是一个坏主意?
最佳答案
如果我理解正确的话,您正在寻找类似 slug 的功能,如果是这样,这里是您应该用来“slugify”ASCII 字符串的函数:
function Slugify($string)
{
return strtolower(trim(preg_replace('~-+~', '-', preg_replace('~[^0-9a-z]~i', '-', $string)), '-'));
}
Slugify('Example - Exampleish (Exampleishbosh)'); // example-exampleish-exampleishbosh
您应该将 slug 存储在数据库中并将其与请求的 URL 进行匹配。
编辑:这个函数可以再简化一点:
function Slugify($string)
{
return strtolower(trim(preg_replace(array('~[^0-9a-z]~i', '~-+~'), '-', $string), '-'));
}
关于php - 有一个 URL 标题来匹配 mysql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1248713/