1

我必须删除字符串中的最后一个元素。我用过rtrimphp但它不起作用。

这是字符串:

 /search/listing.html?vehicle_type=&year=&make_name=&model_name=&loc_type=3&zipcode=641004&distance=100&make_order=ASC

我需要删除“&make_order=ASC”

谁能帮我?

4

9 回答 9

5
$s = '/search/listing.html?vehicle_type=&year=&make_name=&model_name=&loc_type=3&zipcode=641004&distance=100&make_order=ASC';
echo substr($s, 0, strrpos($s, '&'));

编辑:

$url = $base_url.trim( $_SERVER['REQUEST_URI'], "&year_order=".$arr['year_order']."" );
//                                            ^
//                                            |_ replace , with .
于 2013-09-27T12:00:09.503 回答
1

修剪应该工作:

$string = "/search/listing.html?vehicle_type=&year=&make_name=&model_name=&loc_type=3&zipcode=641004&distance=100&make_order=ASC";
$string = trim($string, "&make_order=ASC");
于 2013-09-27T12:01:01.983 回答
1

不能保证make_order它将位于查询字符串的末尾 - 或根本存在。要正确删除该字段,您必须使用以下内容:

$url = '/search/listing.html?vehicle_type=&year=&make_name=&model_name=&loc_type=3&zipcode=641004&distance=100&make_order=ASC';

// break down the URL into a path and query string
$parsed = parse_url($url);

// turn the query string into an array that we can manipulate
$qs = array();
parse_str($parsed['query'], $qs);

// remove the unwanted field
unset($qs['make_order']);

// rebuild the URL
$rebuilt = $parsed['path'];
if(!empty($qs)) {
    $rebuilt .= '?' . http_build_query($qs);
}

echo $rebuilt;
于 2013-09-27T12:07:47.647 回答
0
$actual_link = "/search/listing.html?vehicle_type=&year=&make_name=&model_name=&loc_type=3&zipcode=641004&distance=100&make_order=ASC";


echo str_replace("&make_order=ASC","",$actual_link);
于 2013-09-27T12:01:26.440 回答
0
$string = "/search/listing.html?vehicle_type=&year=&make_name=&model_name=&loc_type=3&zipcode=641004&distance=100&make_order=ASC";

$args = array_pop(explode($string, "&"));

$string = implode("&", $args);
于 2013-09-27T12:01:32.400 回答
0

有很多方法。最简单的可能是:

$i=strrpos($text,'&');
$newstring=substr($text,0,$i);
于 2013-09-27T12:01:43.330 回答
0
$str = "/search/listing.html?vehicle_type=&year=&make_name=&model_name=&loc_type=3&zipcode=641004&distance=100&make_order=ASC";
echo $str . "<br>";
echo trim($str,"&make_order=ASC");
于 2013-09-27T12:05:45.877 回答
0

如果&make_order=ASC总是在最后,你可以使用strstr()来做到这一点

$str = '/search/listing.html?vehicle_type=&year=&make_name=&model_name=&loc_type=3&zipcode=641004&distance=100&make_order=ASC';
echo strstr($str,'&make_order=ASC',true);
于 2013-09-27T12:07:18.817 回答
0

从 url 中删除所需的密钥。

采用:

$s = '/search/listing.html?vehicle_type=&year=&make_name=&model_name=&loc_type=3&zipcode=641004&distance=100&make_order=ASC';
echo remove_key_from_url($url, 'make_order');

输出 :

/search/listing.html?vehicle_type=&year=&make_name=&model_name=&loc_type=3&zipcode=641004&distance=100

代码:

function remove_key_from_url($url, $key) {
    if (strpos($url, '?') === false) return $url;
    list($left, $right) = explode('?', $url, 2);
    parse_str($right, $get);
    if (isset($get[$key])) unset($get[$key]);
    return $left . '?' . http_build_query($get);
}
于 2013-09-27T12:11:26.140 回答