문제

나는 표현에별로 좋지 않습니다 ... 나는 온라인 자습서를 보았지만 여전히 그것을 얻지 못했습니다. 기본적으로 나는 돌아 오려고 노력하고있다 TRUE 문자열이 다음과 같이 형식화 된 경우 :

4 자리 + 공간 + 2 자리를 날짜로 변환합니다.

따라서 문자열은 다음과 같습니다. 2010 02, 그리고 나는 출력하려고합니다 February, 2010.

사용하려고합니다 preg_match, 그러나 나는 계속 받고 있습니다

{수정자가 아닙니다 ...

편집하다

처음 두 응답에 따라 변경했지만 첫 번째 응답이 변경되었지만 두 번째로는 동일한 알 수없는 수정 자 오류에 대해 치명적인 오류를 받고 있습니다.

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
}
도움이 되었습니까?

해결책

문자열이 다음과 같이 형식화되면 True를 반환하려고합니다 : 4 자리 + 공간 + 2 자리

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