Skip to content

Commit 8cd6bd3

Browse files
Carlos Garciaclaude
andcommitted
Fix registro 499 bytes por Ñ en nombres con caracteres multibyte UTF-8
Usa mb_substr y mb_strlen en lugar de substr y str_pad para que el relleno y truncado se hagan por caracteres (no bytes). La Ñ ocupa 2 bytes en UTF-8 pero 1 en ISO-8859-1, por lo que el padding basado en bytes generaba registros de 499 en vez de 500 tras el iconv. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 4c3833d commit 8cd6bd3

2 files changed

Lines changed: 15 additions & 4 deletions

File tree

Lib/Txt347Export.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,22 @@ public static function formatString(string $string, int $length, string $charter
114114
// pasamos el string a mayúsculas
115115
$string = mb_strtoupper($string, 'UTF-8');
116116

117-
// limitamos el tamaño del string
117+
// limitamos el tamaño del string (por caracteres, no bytes)
118118
$string = self::limitString($string, $length);
119119

120120
// rellenamos con el carácter indicado hasta el tamaño indicado, según la alineación indicada
121-
return str_pad($string, $length, $charter, $align);
121+
// usamos mb_strlen para contar caracteres (no bytes) y evitar problemas con ñ, etc.
122+
$currentLength = mb_strlen($string, 'UTF-8');
123+
$padLength = $length - $currentLength;
124+
if ($padLength > 0) {
125+
$pad = str_repeat($charter, $padLength);
126+
if ($align === STR_PAD_LEFT) {
127+
$string = $pad . $string;
128+
} else {
129+
$string = $string . $pad;
130+
}
131+
}
132+
return $string;
122133
}
123134

124135
protected static function loadCompany(): void
@@ -447,7 +458,7 @@ protected static function getSupplierData(): string
447458

448459
protected static function limitString(string $string, int $length): string
449460
{
450-
return substr($string, 0, $length);
461+
return mb_substr($string, 0, $length, 'UTF-8');
451462
}
452463

453464
public static function sanitize(?string $txt): string

facturascripts.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
name = 'Modelo347'
22
description = 'Permite obtener los datos necesarios para el modelo 347 de la hacienda española.'
3-
version = 3.51
3+
version = 3.52
44
min_version = 2025.6

0 commit comments

Comments
 (0)