如何使用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 hereI'd like this output

enter image description here

My code to write in file

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

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

Try something like this:

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

$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;
    }
}