function calculateSmsSegments($text) {
// Alphabet GSM-7 standard + Extension
$gsm7Chars = "@£\$¥èéùìòÇ\nØø\rÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !\"#¤%&'()*+,-./0123456789:;<=>?¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà";
$gsm7Extended = "^{}\\[~]|€";
$isGSM = true;
$length = 0;
// Détection de l'encodage et calcul de la longueur
$chars = preg_split('//u', $text, -1, PREG_SPLIT_NO_EMPTY);
foreach ($chars as $char) {
if (mb_strpos($gsm7Extended, $char) !== false) {
$length += 2; // Compte double
} elseif (mb_strpos($gsm7Chars, $char) !== false) {
$length += 1;
} else {
// Caractère hors GSM-7 trouvé (ex: Arabe, Emoji)
$isGSM = false;
$length = mb_strlen($text, 'UTF-8');
break;
}
}
if ($isGSM) {
if ($length <= 160) return 1;
return ceil($length / 153);
} else {
if ($length <= 70) return 1;
return ceil($length / 67);
}
}
// Tests
echo calculateSmsSegments("Test simple GSM7"); // Retourne 1
echo calculateSmsSegments("Test en arabe مرحبا"); // Retourne 1 (passe en UCS2, mais <70)
echo calculateSmsSegments(str_repeat("مرحبا بكم في خدمة الرسائل القصيرة", 3)); // Retourne 2 ou 3 selon la longueur exacte