Ben
Ben

Reputation: 62336

Remove all characters after "-"

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

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

Upvotes: 0

Views: 346

Answers (2)

Laurence Gonsalves
Laurence Gonsalves

Reputation: 143064

Why were you using non-greedy *? ?

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

Also, the capturing parens were unnecessary.

Upvotes: 2

rjmunro
rjmunro

Reputation: 28056

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.

Upvotes: 4

Related Questions