質問

私は表現と非常によくないよ...私はいくつかのオンラインチュートリアルを見てきましたが、私はまだそれが届きません。基本的に、私は、文字列は次のようにフォーマットされている場合TRUEを返すようにしようとしています:

4桁+スペース+ 2桁、日付に変換します。

だから、文字列は次のようになります。2010 02、と私は、出力February, 2010にしようとしています。

私はpreg_matchを使用しようとしているが、私は得続ける

  

{修飾子ではありません...

の編集

は、最初の2つの応答ごとに、私はそれを変更しましたが、第一及び第二の同じ未知の修飾エラーの致命的なエラーを取得しています:

if(preg_match('/([0-9{4}]) ([0-9]{2})/iU',$path_part)) {
    $path_title = date("F, Y",strtotime(str_replace(" ","-", $path_title)));
}

...また、単に最初応じてより詳細なバージョンを試みたが、エラーが消えている間、それは出力を変更しません。

$path_part = '2010 02';
if(preg_match('/^(\d{4}) (\d{2})$/',$path_part,$matches)) {
   $path_title = $mon[(int)$matches[2]] . " " . $matches[1]; // prints Feb 2010
}
役に立ちましたか?

解決

4桁+スペース+ 2桁

私は、文字列は次のようにフォーマットされている場合は、trueを返しますしようとしています

return preg_match(/^\d{4} \d{2}$/,$input);

あなたのような何かを試すことができ、日付に変換するには

$mon = array('','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
$date_str = "2010 02";

if(preg_match('/^(\d{4}) (\d{2})$/',$date_str,$matches))
{
        print $mon[(int)$matches[2]] . " " . $matches[1]; // prints Feb 2010
}

他のヒント

このいずれかを試してみてください...

preg_match('/([0-9{4}]) ([0-9]{2})/iU', $input);

あなたの実際のコードについてどのような詳細がなくても、次のように動作するはずます:

<?php

$str = '2010 02';

$months = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

if(preg_match('/([0-9]{4}) ([0-9]{2})/', $str, $match) == 1){
    $year = $match[1];
    $month = (int) $match[2];
    echo $months[$month - 1] . ', ' . $year;
}else{
    //Error...
}

?>
$in = "2010 02";
if(preg_match('/([0-9]{4}) ([0-9]{2})/i', $in, $matches)) {
        echo date("F Y", strtotime($matches[2] . "/1/" . $matches[1]));
}

また、 T-REGX のライブラリを使用することができます。

$string = '2010 02';

pattern('\d{4} \d{2}')->match($string)->first(function (Match $match) 
{
    $year = $match->group(1);
    $month = $match->group(2);
});
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top