I recently encountered a situation where I needed to remove the last instance of the character ‘|’ in the menu that was being dynamically generated. So I came up with the following code
<?
$menu = 'I AM |NOT| A BAD |BOY|';
$menu = preg_replace('/[|]([^|]*$)/', '$2', $menu);
echo $menu."\n";
?>
The output would be
I AM |NOT| A BAD |BOY
And that removes the last instance of a character (in this case a pipe sign, i.e ‘|’ ). Hope that helps others.





