고정 필드 텍스트 파일의 데이터
5681 단어 textdatastructurephp
그것은 무엇입니까? 이렇게 엉망이야.
캐릭터 위치에 따라 구성된 데이터:
1 - 11 패킷 ID
12 - 11 애너그래프 ID
23 - 10 제출일
등등.
PHP에서 데이터를 어떻게 읽을 수 있습니까?
첫째, 못생긴 해결책:
substr
를 사용하여 읽기 $identificativo_pacchetto = substr($line,0,11);
$identificativo_mandante = substr($line,11,11);
$data_affido = substr($line,22,10);
...
및
str_pad
쓰기 $line = str_pad($identificativo_pacchetto,11);
$line .= str_pad($identificativo_mandante,11);
$line .= str_pad($data_affido,10);
...
악몽이다!!!
unpack
및 pack
명령으로 시도해 보겠습니다.읽을 코드 한 줄.
$data =unpack('A11identificativo_pacchetto/A11identificativo_mandante/A10data_affido',$line));
데이터는 다음과 같은 배열입니다.
{
"identificativo_pacchetto":"1",
"identificativo_mandante":"52301",
"data_affido":"19-11-2021"
}
작성할 코드 한 줄
$data = ['1','52301','19-11-2021'];
$line =pack('A11A11A10',...$data));
값을 채울 필요가 없습니다.
파일 이름(예: . identificativo_pacchetto)과 구분 기호(
/
)가 없다는 점에 유의하십시오.나는 배열의 구조를 정의하는 모든 것을 함께 넣었습니다.
$def = [
'A11identificativo_pacchetto',
'A11identificativo_mandante',
'A10data_affido
];
$data = ['1','52301','19-11-2021'];
$line = pack(implode('',preg_replace('/^(A\d+).*/','$1',$def)),...$data);
$data = unpack(implode('/',$def),$line);
더 많은 정보:
PHP manual
PHP Cookbook
Stackoverflow
Reference
이 문제에 관하여(고정 필드 텍스트 파일의 데이터), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/abbrevia/data-in-fixed-fields-text-files-5bi6텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)