两个字符串之间的区别和合并

时间:2014-05-10 00:31:00

标签: php string

说我有字符串A:/CI/index.php?/user/dashboard/
字符串B:http://localhost/CI/index.php

使用php如何找到它们之间的通用性/CI/index.php并将其删除,以便我们只留下http://localhost/CI/index.php?/user/dashboard/

1 个答案:

答案 0 :(得分:1)

Bahamondez先生的评论链接对他的具体问题并不起作用。

这是CodeIgniter吗?不会删除CI / index.php'使用htaccess rewrites是一个更容易的选择吗?

我已经把这篇冗长的文章写成了一堆代码(已经很晚了......),我希望能帮助你 - 我相信别人会做得更好将问号带回网址时会出现问题。

$str1 = "http://localhost/CI/index.php";
$str2 = "/CI/index.php?/user/dashboard/";

// Take the url scheme and host out of the equation
$url_array = parse_url($str1 . $str2);

// store the path
$path = $url_array['path'];

// store the query (anything after a ?)
$query = $url_array['query'];

// Combine and split into an array using '/' as delimiter
$segments = explode("/", $path . $query);

// store the unique keys and filter out any empty values
$new_array = array_unique( array_filter($segments) );

var_export($new_array);

结果:

array (
  1 => 'CI',
  2 => 'index.php',
  5 => 'user',
  6 => 'dashboard',
)

然后您可以使用

重建
// Hacky rebuild the url
$segment = "";

foreach ($new_array as $seg) {
    $segment .= $seg . "/";
}

$new_url = $url_array['scheme'] . "://" . $url_array['host'] . "/" . $segment;

哪个会给http://localhost/CI/index.php/user/dashboard/

index.php index.php?会导致问题,但如果您有特定情况,您知道它始终是 / CI / index .php 也许你可以解决这个问题?