tags:

views:

72

answers:

2

I thought this would work but it appears to be only removing the - and the whitespace after it.

$itemList[] = preg_replace('/-(.*?)/i', "", $temp['item']);
+4  A: 

Try:

$itemList[] = preg_replace('/-(.*)$/i', "", $temp['item']);

The $ symbol matches the end of the input, so forces the .* to grab to the end.

Adding a ? after the * makes it un-greedy, meaning it will grab the minimum possible, not the maximum possible, so in this case it is exactly what you don't want.

rjmunro
Actually, removing the `?` should be enough. `.*` will be greedy and match as much as it can. Works either way, though.
Kobi
+2  A: 

Why were you using non-greedy *? ?

$itemList[] = preg_replace('/-.*/i', "", $temp['item']);

Also, the capturing parens were unnecessary.

Laurence Gonsalves