PHP regex match last occurrences of the pattern matching

前端 未结 2 601
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-27 06:28

I know there is a negative lookahead regex that can match last occurrences of the string.

Example: replace last occurrences of string

I want to do something like

2条回答
  •  盖世英雄少女心
    2021-01-27 06:40

    Youre mistake is using \(.*\) it's replace full substring which begins whith ( and ends whith ). .* - max quantificator, (.*) - min quantificator.

    Try to use:

    $string = 'hello example (a) hello example (b) (c)'; echo preg_replace('/\((.*)\)$/', '', $string);

提交回复
热议问题