Reputation: 133
$a="dir=desc&order=position&"
i want to replace =
to -
, &
to -
.
i using the following code:
$a = str_replace('&','-',$a);
$a = str_replace('=','-',$a);
it now turns to dir-desc-order-position-
. but i want to get dir-desc-order-position
. namely, the last character replace with null ""
.
Upvotes: 0
Views: 85
Reputation: 6802
<?php
$a = "dir=desc&order=position&";
//Now replace the character '='
$a = str_replace('=', '-', $a);
$result = str_replace('&', '-', $a);
echo rtrim($result,"-");
?>
More on rtrim()
refer to @Niet the Dark Absol answer.
Upvotes: 1
Reputation: 1575
Remove last character in your string
substr_replace($string ,"",-1);
Upvotes: 0
Reputation: 47
$a = str_replace('&','-',$a,1);
This will replace &
by -
for only once.
Upvotes: 0
Reputation: 760
<?php
$a = "dir=desc&order=position&";
$pattern = '/&/';
$replace = '-';
$b =preg_replace($pattern , $replace, $a );
$c = preg_replace('/=/','-',$b);
echo $c;
?>
Upvotes: 0
Reputation: 44
You can use the trim function if know what character have to remove from string else find the length of your string and use substr function by checking that if how many character needs to be remove from the string end.
Upvotes: 0
Reputation: 324600
You can use rtrim($a,"-")
to remove that character if it exists.
Upvotes: 1