繁体   English   中英

如何获取最后的preg_match函数导致PHP中的字符串

[英]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]”将仅显示字符串中的第一个匹配项。

我想得到最后一场比赛。 “IBCG”

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

如果您想从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]);

显示:IBCG

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM