Kiran George
Kiran George

Reputation: 101

How to remove '<' from a string?

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>'; 
}

Upvotes: 0

Views: 151

Answers (3)

Eiko
Eiko

Reputation: 25632

$x = str_replace("<","",$x);

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

Upvotes: 4

Piskvor left the building
Piskvor left the building

Reputation: 92772

$x = rtrim($x, '<'); // no regex needed

Upvotes: 5

Mike
Mike

Reputation: 21659

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

Upvotes: 1

Related Questions