-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' )

实现这一目标的最佳方法是什么?

4

1 回答 1

2

您可以使用strrpos查找最后一次出现的(,然后您可以使用substrandtrim获取子字符串并将它们修剪为您想要的结果。


例如

/**
 * 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), ' ()')
    );
}
于 2013-02-09T12:32:15.710 回答