开发者

Remove all characters after "-"

开发者 https://www.devze.com 2022-12-11 15:08 出处:网络
I thought this would work but it appears to be only removing the - and the whitespace after it. $itemList[] = preg_replace(\'开发者_运维百科/-(.*?)/i\', \"\", $temp[\'item\']);

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

$itemList[] = preg_replace('开发者_运维百科/-(.*?)/i', "", $temp['item']);


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.


Why were you using non-greedy *? ?

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

Also, the capturing parens were unnecessary.

0

精彩评论

暂无评论...
验证码 换一张
取 消