-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 つだけが必要です (残りの文字列は名前として扱われます)。

期待される出力:

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して最後の出現箇所を見つけ、次にandを(使用して部分文字列を取得し、それらを必要な結果にトリミングできます。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), ' ()')
    );
}
于 2013-02-09T12:32:15.710 に答える