如何从PHP中的字符串中获取正确的法院名称

时间:2015-02-11 00:26:50

标签: php regex

我想从字符串中获取法庭名称。但是我有找到它的问题。通常它位于第一次出现的括号内,但可能位于第二次出现。所以,我试图循环一个数组来检查法院名称,如果不匹配任何法院名称,则打印第二次出现。这是代码:

$string='<b>Yara
Pilbara Fertilisers Pty Ltd [formerly known as Burrup Fertilisers Pty Ltd] v
Oswal (No. 8)</b> (FCA) - cross-vesting - Federal Court
proceedings transferred to Supreme Court of Victoria';

preg_match_all('/\(([^\)]*)\)/', $string, $matches);
    $all_courts=array("HCA","FCAFC","FCA","NSWCA","NSWSC","VCA","QCA","QSC","SASCFC","SASC","TSCA","TSC","WASCA","WASC","ACTCA","ACTSC","NTCA","NTSC");
$court_name=$matches[1][0];

        for($i=0;$i<count($all_courts);$i++)
        {
            if($all_courts[$i]!=$court_name)
            {
                $court_name=$matches[1][1];
            }

        }

echo $court_name;

1 个答案:

答案 0 :(得分:0)

如果我理解正确,你的逻辑就不会做你想要的。它将始终使用第二个括号,因为$ court_name将无法匹配$ all_courts中的一个值。你需要扭转这种逻辑,例如\

if (!in_array($court_name, $all_courts)) {
    $court_name = $matches[1][1];
}