如何在 PHP 中获取字符串中的最后一个 preg_match 函数结果

How to get the last preg_match function result in a string in PHP

我有一个字符串:

$string='Di Cioccio v Official Trustee in Bankruptcy (as Trustee of the Bankrupt Estate of Di Cioccio) (FCAFC) - bankruptcy - shares were after-acquired property which vested in Official Trustee (I B C G)';

我正在使用 preg_match() 函数来获取括号内的值:

preg_match('#\((.*?)\)#', $string, $match);

但是,这里的问题是 "echo $match[0]" 将只显示字符串中的第一个匹配项。

我想获得最后一场比赛,即。 "I B C G"

我首先想到的是从字符串的末尾开始搜索。但是我该怎么做呢?

如果您想找到 preg_match 中的所有匹配项,那么我可以向您展示 preg_match_all() 吗?

它的语法与其他函数基本相同,但会明确查找字符串中的所有匹配项,而不仅仅是第一个。之后,只需要echo end($match);就可以得到最后一个

在@xathien代码的帮助下我整理了这个问题,代码如下:

$string='Di Cioccio v Official Trustee in Bankruptcy (as Trustee of the Bankrupt Estate of Di Cioccio) (FCAFC) - bankruptcy - shares were after-acquired property which vested in Official Trustee (I B C G)';
preg_match_all('#\((.*?)\)#', $string, $match);
echo end($match[1]);

显示: 我 B C G