1

I have to remove the last element in a string. I used rtrim in php but it is not working.

This is the string:

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

I need to remove "&make_order=ASC"

Can anyone help me?

Ander2
  • 5,569
  • 2
  • 23
  • 42
mangala
  • 143
  • 1
  • 2
  • 13

9 Answers9

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, '&'));

Edit:

$url = $base_url.trim( $_SERVER['REQUEST_URI'], "&year_order=".$arr['year_order']."" );
//                                            ^
//                                            |_ replace , with .
Community
  • 1
  • 1
Glavić
  • 42,781
  • 13
  • 77
  • 107
  • no its not working it showing empty result – mangala Sep 27 '13 at 12:02
  • My example is working. You probably used it in different way. Can you copy/paste how you used it? – Glavić Sep 27 '13 at 12:03
  • $url = $base_url.trim( $_SERVER['REQUEST_URI'], "&year_order=".$arr['year_order']."" ); tried this.. but not working i am getting /search/listing.html?vehicle_type=&year=&make_name=&model_name=&loc_type=3&zipco‌​de=641004&distance=100&make_order=A – mangala Sep 27 '13 at 12:13
  • In `trim()` you have `,` insterad of `.`. – Glavić Sep 27 '13 at 12:16
1

trim should work:

$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");
  • $url = $base_url.trim( $_SERVER['REQUEST_URI'], "&year_order=".$arr['year_order']."" ); tried this.. but not working i am getting /search/listing.html?vehicle_type=&year=&make_name=&model_name=&loc_type=3&zipcode=641004&distance=100&make_order=A – mangala Sep 27 '13 at 12:10
  • 1
    This may happen to work, given the example.. but `trim` keeps on stripping bytes from the first argument that appear in the second argument until it doesn't. `trim` does not strip the string as-is. – Evert Sep 27 '13 at 12:20
1

There's no guarantee that make_order will be at the end of the query string - or exist at all. To remove the field properly, you'd have to use something like this:

$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;
George Brighton
  • 5,131
  • 9
  • 27
  • 36
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);
M Shahzad Khan
  • 935
  • 1
  • 9
  • 22
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);
alandarev
  • 8,349
  • 2
  • 34
  • 43
0

There are a bunch of ways. The easiest might be:

$i=strrpos($text,'&');
$newstring=substr($text,0,$i);
mlewis54
  • 2,372
  • 6
  • 36
  • 58
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");
0

if &make_order=ASC is always going to be at the end, you can use strstr() to do this

$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);
bansi
  • 55,591
  • 6
  • 41
  • 52
0

Remove desired key from url.

Use:

$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');

Output :

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

Code:

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);
}
Glavić
  • 42,781
  • 13
  • 77
  • 107