场景如您所知,StackOverflow会检查问题中的标题。我的意思是当你打开这个URL时:
http://stackoverflow.com/questions/38839016/should-i-store-the-result-of-an-function
自动将其替换为:
http://stackoverflow.com/questions/38839016/should-i-store-the-result-of-an-function-into-an-array
该替换是因为第一个URL不完整。
好的,我正试图通过PHP制作这样一个系统。这是我的尝试:
// getting this from the URL
$id = '38839016';
// fetching this from database based on that id (38839016)
$real_title = $result['title'];
//=> Should I store the result of an function into an array
$real_title = str_replace("-"," ",strtolower($real_title));
//=> should-i-store-the-result-of-an-function-into-an-array
// getting whole uri
$current_uri = $_SERVER["REQUEST_URI"];
//=> /questions/38839016/should-i-store-the-result-of-an-function
我想做什么:我需要将$real_title
与$current_uri
的标题部分进行比较。如何确定" title-part" ?在$id.'/'
或/
或?
(字符串结尾)之前,$
之后的所有内容都是如此。我该如何进行比较?
然后:
if ( preg_match("//", $current_uri) ) {
// all fine. continue loading the page
continue;
} else {
// replace title-part of the $current_uri with $real_title
preg_replace("//", $real_title, $current_uri);
// redirect to this post with correct slug
header('Location: '.$_SERVER["HTTP_HOST"].);
}
简单地说,我想完成这些:
if ( preg_match("//", $current_uri) ) {
preg_replace("//", $real_title, $current_uri);
答案 0 :(得分:1)
好的,简单来说,有一个好的网址和一个请求的网址
如果请求的网址不等于好网址
将访问者重定向到好人
<?
$id = '38839016';
$good_url = '/questions/'.$id.'/'.str_replace("-"," ",strtolower($result['title']));
preg_match( '/\/[0-9]+\/([a-z0-9-]+)\/.*/', $_SERVER[REQUEST_URI], $matches);
if ($matches[1] != str_replace("-"," ",strtolower($result['title'])))
header('Location: '.$good_url);