Szenario: Wie Sie wissen, überprüft StackOverflow den Titel in der Frage. Ich meine, wenn Sie diese URL öffnen:Wie kann ich die Post Slug überprüfen?
http://stackoverflow.com/questions/38839016/should-i-store-the-result-of-an-function
automatisch wird es mit dieser Fassung:
http://stackoverflow.com/questions/38839016/should-i-store-the-result-of-an-function-into-an-array
Das Ersatz wegen unvollständig die erste URL ist.
Ok gut, ich versuche, ein solches System von PHP zu machen. Hier ist mein Versuch :
// 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
Was ich tun möchte: ich $real_title
mit Titel-Teil des $current_uri
vergleichen müssen. Wie kann ich feststellen "Titel-Teil"? Es ist alles, was nach $id.'/'
ist, bis /
oder ?
oder $
(Ende der Zeichenfolge). Wie kann ich diesen Vergleich machen?
Und dann:
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"].);
}
kurz, ich will diese vervollständigen:
if (preg_match("//", $current_uri)) {
preg_replace("//", $real_title, $current_uri);
Diese Lösung schlägt fehl, wenn es einige übergebenen Parameter als GET-Methode ist, wie folgt aus:? 'Arg1 = value1 & arg2 = value2' –
@MartinAJ Sorry, ich diese Antwort hinzugefügt, weil Sie die Stackoverflow Beispiel hinzufügen – Mostafa
Sind Sie wollen um nur den Slug in der URL zu korrigieren, ohne irgendeine andere Syntax in der URL zu ändern? – Mostafa