2013-02-09 124 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