Created
February 3, 2020 08:13
-
-
Save MSHADroo/b1b1783c1821aa6224dd2f3ae3c8fc8f to your computer and use it in GitHub Desktop.
php check persian and english sms length
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function checkSMSLength($text) { | |
$ucs2 = preg_match('/[^\x00-\x7E]/', $text); | |
if (!$ucs2){ | |
$text = preg_replace('/([[\]{}~^|\\\\])/', "\\$1",$text); | |
} | |
$strLength = mb_strlen($text); | |
$unitLength = $ucs2 ? 70 : 160; | |
if ($strLength > $unitLength) { | |
$unitLength = $ucs2 ? $unitLength - 3 : $unitLength - 7; | |
} | |
$count = max(ceil($strLength / $unitLength), 1); | |
$remain = $unitLength * $count - $strLength; | |
return [ | |
'count' => $count, | |
'remain' => $remain | |
]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment