[PHP] DateTime:: Unexpected character 에러 해결하기
·
Development/PHP
DateTime::__construct(): Failed to parse time string ( 13 March 2023) at position 0 (�): Unexpected character 사용한 코드는 아래와 같다. 날짜 변환함수를 사용하는데, 위와 같은 에러가 발생하였다. try { $strCommentDate = new DateTime($strCommentDate); } catch(Exception $e) { echo $e->getMessage(); } 예기치 않은 문자(인쇄할 수 없거나 인식할 수 없는 문자)가 포함되어있어서 실패하게 된 것이다. 다른 여러 방법들도 있겠지만, 정규식을 통해 해결하였다. preg_replace('/[^\x20-\x7E]/', '', $text); 위 정규식은..