2013-02-09 125 views
-2

這是我的第一個問題,我弄亂了字符串。我有以下格式的一些字符串:括號內括號內的獨立字符串

 I will be here (I may or may not be here) (30-Apr-2013) 
    I am still here (15-Feb-2013) 
    I am still here(I may not be here) (I may not be here) (9-Apr-2013) 

我需要從名稱中分隔日期。正如你所看到的,括號的數量可能會有所不同,但我只需要最後一個(字符串的其餘部分將被視爲名稱)。

預期輸出:

1. array(0=> 'I will be here (I may or may not be here)' , 1=> '30-Apr-2013') 
2. array(0=> 'I am still here' , 1=> '15-Feb-2013') 
3. array(0=> ' I am still here(I may not be here) (I may not be here)' , 1=> '9-Apr-2013') 

什麼是實現這一目標的最佳途徑?

+0

@negvoters關心解釋這個問題有什麼問題? – Gogol 2016-03-15 07:24:30

回答

2

您可以使用strrpos找到(最後出現的,然後你可以使用substrtrim獲得子串,並將它們修剪到你想要的結果。


例如,

/** 
* Return an array with the "name" as the first element and 
* date as the second. 
*/ 
function fun($string) 
{ 
    $datePos = strrpos($string, '('); 
    return array (
     trim(substr($string, 0, $datePos - 1)), trim(substr($string, $datePos), '()') 
    ); 
} 
+1

你打敗了我。我只是添加一個例子,如果你不介意.. – 2013-02-09 12:52:56

+1

@czarpino謝謝,我想我會讓OP做到這一點:順便說一句,我添加了一個字符列表「修剪」,因爲這是我提到它的原因... – jeroen 2013-02-09 12:57:09

+0

啊,是的,多麼尷尬。我才發現。 – 2013-02-09 12:58:06