- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
任何人都可以建议在 php 中的方法或不涉及 htaccess 或 mod_rewrite 的解析 SEO 友好 url 的函数吗?例子会很棒。
http://url.org/file.php/test/test2#3
返回:Array ( scheme] => http [host] => url.org [path] =>/file.php/test/test2 [fragment] => 3 )/file.php/test/test2
我将如何分离出/file.php/test/test2 部分?我想 test 和 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>";
最佳答案
您可以使用 explode 从数组中获取零件:
$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,在父列中将其与数组的下一个值一起使用。取消设置 $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。只需继续将变量传递回来 c
有一天我掌握了递归的诀窍 ^^。
再次编辑:糟糕,原来是一些代码。
关于php - 在没有 htaccess 或 mod_rewrite 的情况下解析 SEO 友好的 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20076144/
为了我们公司的 SEO 需求,我们需要通过 301 将一些 SEO URL 更改为另一个 URL。 示例: /seo/keywords-1-2-3/ 到 /seo/mynew301page-1-2-3
使已经开发的 Vue 应用程序对 SEO 友好的最简单方法是什么(搜索引擎可以读取所有标题元标记)。 我已经研究过 NUXT,我将不得不再次重新构建整个过程,而且除了 SSR(服务器端渲染)部分之外,
我正在使用 Azure 数据库进行一些数据库管理,我需要执行查询,例如删除数据库中的所有约束。 sp_MSForEachTable 在使用 Azure 数据库时不可用,因此我正在采用不同的方法来实现此
在我的研究中,我发现了 2 种方法。 两者都需要修改 Global.Asax 中的 Application_BeginRequest 过程,您可以在其中运行代码来进行实际的 URL 映射(我的数据库
例如我有这个字符串: make no@ sen# `se ! 我会像这样生成 url make-no-sen-se! 我有这个: var value = $('.titleVal').val(
我有一个网站,我想开始使用友好的 URL。 所以不是: http://server.com/company.php?id=12 我可以使用: http://server.com/company/ste
我正在用 pandas 编写一些数据分析管道。我一直在使用的数据框中的一列是由自定义编写的类的对象组成的,每个类都用一个字符串初始化,我用正则表达式从中读取各种信息并存储在对象的属性中。子类结构类似于
这个问题已经存在: C++ create new windows command [closed] 关闭 4 年前。 我正在尝试创建一个 C++ 控制台应用程序并创建新命令。 我想做的事的例子:在终
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 8 年前。 Improve
我一直在尝试为 prestashop 中的产品创建第二个 url 重写选项.在 SEO 和 URL 部分的标准 Prestashop 安装中,我得到了以下产品 url 构建: {category:/}
我在使用重写引擎通过友好的 url 实现我的目标时遇到了一些问题。 基本上我有以下结构:- 索引.php - 吃下 - index.php 我在主索引中的链接是这样的:下载/index.php?dow
我真的不喜欢在链接中使用选择/选项元素: 但是他们已经找到了作为一种选择进入设计师组合的方式,并尽我所能劝阻他们不要使用它们,我知道如果没有谷歌的力量,我会输掉这场战斗,而且“这对 SEO 不利”。
dispose: function() { var disposer; while (disposer = this._disposers.shift()) { // expected
我将 Heroku 应用程序上的自定义域更改为新域。现在我将创建一个新的 Heroku 应用程序,它的唯一目的是重定向到第一个应用程序。 我在 Google Webmasters 中读到我应该像这样进
我有一个类。这个类有2个字段。 我有一个整数数组。我的任务是重载友好的“+”运算符,以便可以将数组中的字段值与数组元素的值相加。 例如: class Test { public: double
这个问题在这里已经有了答案: Encode URL in JavaScript (22 个答案) 关闭 9 年前。 您好,我有这个 URL(顶部),我正在尝试与“pinterest”一起使用,他们会
jQuery 模式对话框 seo 友好吗?如果不是,我们可以进行哪些更改,以便爬虫可以轻松读取模态对话框中的内容? 提前致谢。 编辑:抱歉忘记提及模式中的内容来自服务器端。所以它也使用 ajax。 最
这是我的 main.js 文件: jQuery(function($) { $('LI.tree-item-name').has('ul').click(function() { if (
static func randomShape() -> Shape { // Find out count of possible shapes var maxValue = 0
我正在构建一个通过 index.php 运行所有代码的网站。 例如 index.php?controller=something&id=01234。 我想使用 PHP 创建友好的 URL,所以我这样做
我是一名优秀的程序员,十分优秀!