Bruno Justino Praciano
Bruno Justino Praciano

Reputation: 552

How to limit a number of characteres in txt using PHP

I would like a way to break the line when it gets to 360 characters in a txt file.

Example:

In my script my output

enter image description here I'd like this output

enter image description here

My code to write in file

$string = $data.'*'."\n";

$fp = fopen( 'registro.txt', 'a+' );
if( !$fp ){
  echo 'Erro inesperado, não foi possivel abrir o arquivo';
  exit;
}else{
   fwrite( $fp, $stringSEFIP."\n");
}

Upvotes: 0

Views: 64

Answers (1)

Claudio
Claudio

Reputation: 5203

Try something like this:

$string = $data.'*'."\n";

$skip = 360;

$fp = fopen( 'registro.txt', 'a+' );
if( !$fp ){
  echo 'Erro inesperado, não foi possivel abrir o arquivo';
  exit;
}else{
    $pos = 0;
    while ($pos < strlen($str)) {
        fwrite( $fp, substr($stringSEFIP, $pos, $skip) . PHP_EOL);
        $pos += $skip;
    }
}

Upvotes: 1

Related Questions