在没有htaccess或mode_rewrite的情况下解析SEO友好的url


parsing SEO friendly url without htaccess or mode_rewrite

有人能在php中为parsingEO友好的url建议一个不涉及htaccess或mod_rewrite的方法或函数吗?举个例子太棒了。

http://url.org/file.php/test/test2#3

返回:Array(scheme]=>http[host]=>url.org/path]=>/file.php/test/test2[frage]=>3)/file.php/test/test 2

如何将/file.php/test/test2部分分开?我想test2和test2会是争论。

编辑:

@Martijn-在收到关于你答案的通知之前,我确实弄清楚了你的建议。顺便说一句,谢谢。这被认为是一种可以的方法吗?

$url = 'http://url.org/file.php/arg1/arg2#3';
$test = parse_url($url);
echo "host: $test[host] <br>";
echo "path: $test[path] <br>";
echo "frag: $test[fragment] <br>";
$path = explode("/", trim($test[path]));
echo "1: $path[1] <br>";
echo "2: $path[2] <br>";
echo "3: $path[3] <br>";
echo "4: $path[4] <br>";

您可以使用爆炸从数组中获取零件:

$path = trim($array['path'], "/"); // trim the path of slashes
$path = explode("/", $path);
unset($path[0]); // the first one is the file, the others are sections of the url

如果你真的想让它再次以零为基础,请将其添加为最后一行:

$patch = array_values($path);

针对您的编辑:
你想让它尽可能灵活,所以没有基于最多5个项目的固定编码。尽管你可能永远不会超过这个数字,但不要把自己束缚在它上面,只是你不需要的开销。

如果你有这样的页面系统:

id parent  name                url
1   -1      Foo                 foo
2    1      Bar, child of Foo   bar-child-of-foo

制作一个递归函数。将数组传递给一个函数,该函数占用第一个部分来查找根项

SELECT * FROM pages WHERE parent=-1 AND url=$path[0]

该查询将返回一个id,在父列中使用该id和数组的下一个值。取消设置$path数组的每个找到的值。最后,您将拥有一个包含其余部分的数组。

绘制示例:

function GetFullPath(&$path, $parent=-1){
    $path = "/"; // start with a slash
    // Make the query for childs of this item
    $result = mysqli_query($conn, "SELECT * FROM pages WHERE parent=".$parent." AND url=".current($path)." LIMIT 1");
    // If any rows exists, append more of the url via recursiveness:
    if($result->num_rows!==0){
        // Remove the first part so if we go one deeper we start with the next value
        $path = array_slice($patch,1); // remove first value
        $fetch = $result->fetch_assoc();
        // Use the fetched value to go deeper, find a child with the current item as parent
        $path.= GetFullPath($path, $fetch['parent']);
    }
    // Return the result. if nothing is found at all, the result will be "/", probs home
    return $path;
}
echo GetFullPath($path); // I pass it by reference, any alterations in the function happen to the variable outside the scope aswell

这是一个草稿,我没有测试这个,但你知道我试图草图的想法。你可以使用相同的方法来获得你所在页面的ID

有一天,我掌握了反复出现的窍门
再次编辑:哎呀,原来是一些代码