如何在PHP中从文件中分割和计数sms消息

时间:2014-02-11 14:29:25

标签: php mysql csv sms-gateway

我的问题是我的sms消息是用csv导入的,然后检查数字是否正常以及短信是多长时间。我的问题是,如果文本消息长于160,它仍然在数据库中输入1。但它应该开始计数,如果它小于或等于160,它是1条消息,如果它大于160但小于或等于320则是两条消息,如果它是更多则它是3条消息。 页面代码在这里:

<?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 '';
}
}
?>

有人可以帮我解决这个问题吗? 谢谢

1 个答案:

答案 0 :(得分:1)

尝试

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

而不是

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