質問

私の処理の文字列と日付のどこかにします。のあり方にあり、期日までがこの文字列は:

"... 01.11.2009 18:00-21:00 ..." または "... 01.11.2009 18:00-02.11.2009 15:00 ..." または "...01.11.2009 18:00までとさせていた---"

を問わずどのように日付が表示されます初日の"01.11.2009 18:00"になります。である場合は二つの試合でつなぎ合わせます。かを分離/爆発からの文字列はクリアしました。ずっと面白いアイデアないか?

思いで作られたパターンを正規表現をマッチングでpreg_match.これそうです。残念ながらいませんへの正規表現です。が誰にも助けを得るのが私の一日のブロックからランダムな文字列?

役に立ちましたか?

解決

$matches = array();
$desired_date = '';
preg_match('/\d{2}\.\d{2}\.\d{4} \d{2}:\d{2}/', $string_containing_dates, $matches);
if (isset($matches[0])) $desired_date = $matches[0];

他のヒント

う:

$s = "… 01.11.2009 18:00-21:00 …… 01.11.2009 18:00-02.11.2009 15:00 …… 01.11.2009 18:00 …";
preg_match_all('!(\d{2}\.\d{2}\.\d{4}) \d{2}:\d{2}(-\d{2}:\d{2}|-\d{2}\.\d{2}\.\d{4} \d{2}:\d{2})?!', $s, $matches);
print_r($matches[1]);

ご日程のフォーマットを以下のようにまと同じ数の文字毎日です。きに利用し、簡単にsubstr()の先頭X字:

// example date strings
$date = date('m.d.Y h:i:S');
$date2 = date('m.d.Y h:i:S', strtotime('+50 days'));
$date_str = $date . '-' . $date2;

// get the first 10 characters for the date
$match = substr($date_str, 0, 10);

う:

preg_match_all(
    '/([0-9]{2}\.[0-9]{2}\.[0-9]{4} [0-9]{2}:[0-9]{2})' // linebreak added
    . '(?:-(?:[0-9]{2}\.[0-9]{2}\.[0-9]{4} )?(?:[0-9]{2}:[0-9]{2})?)?/',
    '" 01.11.2009 18:00-21:00 " or " 01.12.2009 18:00-02.12.2009 15:00 " '
    . 'or " 01.01.2009 18:00 "',
    $matches
);

print_r($matches[1]);
// "01.11.2009", "01.12.2009", "01.01.2009"

を抽出しますの先頭の日付フォーマットを以下の機能:

function find_date($string) {
    preg_match("/\d{2}\.\d{2}\.\d{4} \d{2}:\d{2}/",$string,$matches);
    return $matches[0];
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top