Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从原始输入生成URL段塞(SEO URL)_Php_Url_Replace_Format - Fatal编程技术网

Php 如何从原始输入生成URL段塞(SEO URL)

Php 如何从原始输入生成URL段塞(SEO URL),php,url,replace,format,Php,Url,Replace,Format,现在,当用户在my php Web应用程序中按下一个链接时,他们会被发送到一个url如下的页面:domain.com/faq/item/18483/或domain.com/blog/item/37476。但我想为这些url添加一个标题,这样用户就可以知道链接背后的内容(例如:我与朋友共享一个链接),如下所示:domain.com/faq/item/18483/I-have-a-question 如果我有这样的标题: 这是一个常见问题 博客®!@#留言&你好& 清理并将其转换为此格式的最佳方法

现在,当用户在my php Web应用程序中按下一个链接时,他们会被发送到一个url如下的页面:
domain.com/faq/item/18483/
domain.com/blog/item/37476
。但我想为这些url添加一个标题,这样用户就可以知道链接背后的内容(例如:我与朋友共享一个链接),如下所示:
domain.com/faq/item/18483/I-have-a-question

如果我有这样的标题:

  • 这是一个常见问题
  • 博客®!@#留言&你好&
清理并将其转换为此格式的最佳方法是什么:

  • 这是一个常见问题
  • 博客留言你好

实际上,就像stackoverflow对我的标题所做的那样,我想创建如下URL:
http://stackoverflow.com/questions/22042539/how-to-generate-url-slugs-seo-urls-from-raw-input/

您可以使用正则表达式将所有非字母数字的内容更改为连字符:

function slugify($string) {
    // Make the whole string lowercase
    $slug = strtolower($slug);
    // Replace utf-8 characters with 7-bit ASCII equivelants
    $slug = iconv("utf-8", "ascii//TRANSLIT", $input);
    // Replace any number of non-alphanumeric characters with hyphens
    $slug = preg_replace("/[^a-z0-9-]+/", "-", $string);
    // Remove any hyphens from the beginning & end of the string
    return trim($slug, "-");
}
示例字符串的输出为:

echo slugify("this is a faq item");         // this-is-a-faq-item
echo slugify("blog ®!@# message &hello&");  // blog-message-hello

这个问题有一些建议:哦,刚刚尝试过这个:但你的答案仍然是对的。@sirwilliam添加了两个步骤来将slug小写,并用7位ASCII等效字符替换国际字符(重音、umlaut等)。