Php 括号内的单独字符串,括号数量不同
这是我在这里的第一个问题,我搞砸了字符串。我有以下格式的字符串:Php 括号内的单独字符串,括号数量不同,php,string,pattern-matching,Php,String,Pattern Matching,这是我在这里的第一个问题,我搞砸了字符串。我有以下格式的字符串: 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) 我需要把日期和名字分开。正如您所看到的,括号的数量可能会有所不同,但我只需要最后一个括号(
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' )
实现这一目标的最佳方式是什么 您可以使用查找上次出现的(
),然后可以使用和来获取子字符串,并将其修剪为所需的结果
例如
@czarpino谢谢,我想我应该让OP来做:-)顺便说一句,我在
trim
中添加了一个字符列表,因为这就是我提到它的原因…啊,是的,太尴尬了。我才意识到,它就像一个符咒!非常感谢@jeroen:-)@Neg选民愿意解释这个问题的问题吗?
/**
* 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), ' ()')
);
}