php - How can I check the post slug? -


scenario: know, stackoverflow checks title in question. mean when open url:

http://stackoverflow.com/questions/38839016/should-i-store-the-result-of-an-function 

automatically replaced this:

http://stackoverflow.com/questions/38839016/should-i-store-the-result-of-an-function-into-an-array 

that replacement because of being incomplete first url.


ok well, i'm trying make such system php. here my attempt:

// getting url $id = '38839016';  // fetching database based on id (38839016) $real_title = $result['title']; //=> should store result of function 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 

what want do: need compare $real_title title-part of $current_uri. how can determine "title-part"? after $id.'/' until / or ? or $ (end of string). how can comparison?

and then:

if ( preg_match("//", $current_uri) ) {     // fine. continue loading page     continue; } else {     // replace title-part of $current_uri $real_title            preg_replace("//", $real_title, $current_uri);               // redirect post correct slug     header('location: '.$_server["http_host"].); } 

briefly, want complete these:

if ( preg_match("//", $current_uri) ) {  preg_replace("//", $real_title, $current_uri);  

ok, in simple words, there url , requested url

if requested url not equal url

redirect visitor one

<? $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); 

Comments