简体   繁体   English

PHP - 如何用空格替换短划线?

[英]PHP - How can I replace dashes with spaces?

I am currently using the following code to convert my strings to seo friendly urls: 我目前正在使用以下代码将我的字符串转换为seo友好的URL:

        function url($url) {
    $url = str_replace(" ", " ", $url);
    $url = str_replace(array("'", "-"), "", $url); 
    $url = mb_convert_case($url, MB_CASE_LOWER, "UTF-8");
    $url = preg_replace("#[^a-zA-Z]+#", "-", $url); 
    $url = preg_replace("#(-){2,}#", "$1", $url); 
    $url = trim($url, "-"); 
        return $url;
} 

When I query my database I match the url against the article titles in my database, my problem is that after performing the seo friendly url function the urls do not match any article titles in my database. 当我查询我的数据库时,我将url与我数据库中的文章标题相匹配,我的问题是在执行seo friendly url函数后,url与我的数据库中的任何文章标题都不匹配。

The addition of dashes (not sure about the lowercase) means that they are completely different to the entries in the database. 添加破折号(不确定小写)意味着它们与数据库中的条目完全不同。

What is my next step, should I remove the dashes before querying the database, if so how? 我的下一步是什么,我应该在查询数据库之前删除破折号,如果是这样的话?

Or is it better practice to include the article id in my url somewhere and reference it? 或者更好的做法是将文章ID包含在我的网址中并引用它?

Querying by id seems far faster and simplier to me than reconverting back your titles, using url rerwriting to ignore the title (just for referencement) and call a page with the id as a GET argument. 通过id查询似乎比重新转换标题更快更简单,使用url重写忽略标题(仅供参考)并调用id为GET参数的页面。 Looking at the current URL let me think that StackOverflow works this way. 查看当前的URL让我认为StackOverflow以这种方式工作。

Using the current page as an example, i suspect that 以当前页面为例,我怀疑

http://stackoverflow.com/questions/8034788/php-how-can-i-replace-dashes-with-spaces

is rewritten to something like 被改写成类似的东西

http://stackoverflow.com/questions.php?id=8034788

where a simple SQL query gets the content of the article. 其中一个简单的SQL查询获取文章的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM