简体   繁体   English

如何在PHP中从文件拆分和计数短信

[英]How to split and count sms messages from file in PHP

I have problem that my sms messages are imported with csv, then it is checked if number is ok and how long sms is. 我的问题是我的短信是用csv导入的,然后检查号码是否正确以及短信有多长时间。 My problem is that if text messages is longer then 160 it still enters 1 in databse. 我的问题是,如果短信长于160,则它仍将在数据库中输入1。 But it should start counting, if it is less or equal than 160, it is 1 messages, if it is more than 160 but less or equal than 320 it is two messages and if it is more then it is 3 messages. 但是它应该开始计数,如果小于或等于160,则为1条消息;如果大于160但小于或等于320,则为2条消息;如果大于或等于3,则为3条消息。 Page code is here: 页面代码在这里:

<?php
$link = @mysql_connect("localhost", "admin", "") or die("Error: Database offline.");
mysql_select_db("database", $link);
mysql_query("SET NAMES 'utf8' ", $link);

function detect_type($smstext) {
$type = 0;
$dec_codes = array();
for ($i = 0; $i < strlen($smstext); $i++) {
    $symbol = substr($smstext,$i,1);
    if (!in_array(ord($symbol), $dec_codes)) { $type = 1; }
}
return $type;       
}
$result_array = array();
$unic_numbers = array();
$fp = file_get_contents($_FILES['filename']['tmp_name']);
$fp = str_replace("\r\n", "\n", $fp);
$fp = str_replace("\r", "\n", $fp);
$fp  = str_replace("\t", "", $fp);  
$rows = explode("\n", $fp);             
$imported_rows = 0;
$duplicate_rows = 0;
$error_rows = 0;
$long_rows = 0;
for ($i = 0; $i < sizeof($rows); $i++) {    
$data = explode(";", $rows[$i]);
$data[1] = sms_formatNumbers($data[1]); // formating number
$userid = 78;
    if(strlen($data[1]) > 9){ 
        if($unic_numbers[$data[1]] != true ){  // unic number check
            $unic_numbers[$data[1]] = true;
            $imported_rows++;
            $fullSMS =  iconv("ISO-8859-1","UTF-8", trim($data[2])." ".trim($data[3])." ".trim($data[4]));
            if(strlen($fullSMS) > 164){
                $long_rows++;
            }           
    if($_POST['action'] == 'send'){                 
            // SMS TEXT 
            $smstext = str_replace("õ", "ò", $fullSMS);
            $smstext = str_replace("Õ", "ò", $smstext);
            $type = detect_type($smstext);                  
            // servicegroup             
            $char2 = substr($data[1], 0, 2);
            $char3 = substr($data[1], 0, 3);                                                                    
            $c1 = mysql_query("SELECT * FROM zone_info WHERE country_code = '".$char2."'", $link);
            $c2 = mysql_query("SELECT * FROM zone_info WHERE country_code = '".$char3."'", $link);
            if (mysql_num_rows($c1) == 1) {
                $r = mysql_fetch_array($c1);
                $price = $r['price'];
                $z = mysql_query("SELECT * FROM zone WHERE id = ".$r['up']."", $link);
                $zone = mysql_fetch_array($z);
                $zone_id = $zone['id'];
                $servicegroup = $zone['servicegroup'];
            } else if (mysql_num_rows($c2) == 1) {
                $r = mysql_fetch_array($c2);
                $price = $r['price'];
                $z = mysql_query("SELECT * FROM zone WHERE id = ".$r['up']."", $link);
                $zone = mysql_fetch_array($z);
                $zone_id = $zone['id'];
                $servicegroup = $zone['servicegroup'];
            }   
            require_once("../scripts/number.class.php");
            $receiver = "00".$data[1];      
            $obj = new NumberClass($receiver);
            $operator = $obj -> operator_code;
            $country = $obj -> code;
            $operator_name = $obj -> operator_name;             
        if(strlen($operator) > 0) {
        $er = mysql_query("SELECT * FROM zone_exception WHERE country = ".$country." AND operator = ".$operator."", $link);
        if (mysql_num_rows($er) == 1) {
            $erand = mysql_fetch_array($er);
            $price = $erand['price'];
            $servicegroup = $erand['servicegroup'];                                                         
        }
    } else $operator_name = "-";
    if ($operator_name == "-") { $servicegroup = $servicegroup; }
    else {
        if ($operator_name == " First Operator") $servicegroup = "90";
        else if ($operator_name == "Second Operator") $servicegroup = "91";
        else if ($operator_name == "Third Operator") $servicegroup = "92";
        else $servicegroup = $servicegroup;
    }
    require_once("../core/init.mini.inc.php");      
    $servicegroup = UserBasedRerouting($receiver, $userid, $operator_name, $servicegroup);                  
            $client_type ='corporative';
            $sender = $data[0];             
            $zone_id = 11;
            $client_sms_id = '0';
            $client_want_report = '0';
            $client_report_url = '';
            $amount = 1;
            $dt_delaysend = '1970-01-01 00:00:00';
            $SMSsent = 0;
            $SMStotal = 1;
            $smstext_old = $smstext;            
            while($SMSsent < $SMStotal){    
        $sql = mysql_query("insert into sms_queue (user_id,client_type,dt_entered,sender,receiver,operator,smstext,sms_type,zone_id,client_sms_id,client_want_report,client_report_url,sms_price,amount,servicegroup,dt_delaysend) values ('$userid','$client_type','".date('Y-m-d H:i:s')."','$sender','$receiver','$operator_name','$smstext',0,'$zone_id','$client_sms_id','$client_want_report','$client_report_url','$price','$amount','$servicegroup','$dt_delaysend')", $link);
                $SMSsent++;
            }               
    }
        }else{
            $duplicate_rows ++;
        }

    }else{
        $error_rows++;
    }
}
$result_array['success'] = true;
$result_array['long_sms'] = $long_rows;
$result_array['send_sms'] = $imported_rows;
$result_array['error_sms'] = $error_rows;
$result_array['duplicate_sms'] = $duplicate_rows;
$result_array['action'] = $_POST['action']; 
echo json_encode($result_array);        
function sms_formatNumbers($number){    
$number = (int)$number; 

$start_code = (int)substr($number,0,4);
if($start_code < 3780 or $start_code == 3785 or $start_code > 3789){
    return $number;
}else{
    return '';
}
}
?>

Can someone help me out with that? 有人可以帮我吗? Thank you 谢谢

Try 尝试

if(strlen($fullSMS) > 164){
       $long_rows = ceil(strlen($fullSMS)/160);
} 

instead of 代替

if(strlen($fullSMS) > 164){
        $long_rows++;
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM