views:

55

answers:

3

say, i have a string like $x="History[424]<"; how to remove the last "<" and make the string $x="History[424]"; ... I tried str_replace and don't know, its not working... :(. Thx in advance

for($k=0;$k<$i;$k++) { 
    $linklabelmod[$k] = str_replace($linklabel[$k], $linklabel[$k]."[$k]", $linklabel[$k]); 
    //$var= str_replace($linklabel[$k], $linklabelmod[$k], $var); 
    print $linklabelmod[$k].'<&nbsp;&nbsp;&nbsp;'; 
    //print $linklabel[$k].'&nbsp;&nbsp;&nbsp;'; 
    print $link[$k].'<br>'; 
}
+4  A: 
$x = str_replace("<","",$x);

Edit: This replaces all of the "<", but as you mentioned str_replace in your question, this is how it works.

Eiko
+1  A: 

This would ensure that < is only ever removed from the end of the string, and not from anywhere else within the string;

$y = preg_replace('/<$/', '', $x );
Mike
thx , this was exactly i was looking for
Kiran George
Piskvor's answer is better, as it avoids the need for a regular expression - you should use that instead.
Mike
+5  A: 
$x = rtrim($x, '<'); // no regex needed
Piskvor
+1 Good point :-)
Mike
thx for this also.
Kiran George