2

I am experiencing a little problem with my PHP function.

I created a PHP function to deduct 1 SMS credit after 160 characters, 2 SMS credits after 320 characters and so on, but recently I observed that a text of 151 characters or less than 160 deducts 2 SMS credits. I don't know if there is any hidden character or if I did not escape certain strings.

Below is the PHP code:

//=== function to deduct credit per sms

function deduct_sms_credit( $user_id, $msg ) {
    $per_sms = 160;

    //calculate the no. Of sms pages with ceil() function
    $no_pg_sms = ceil( strlen( $msg ) / $per_sms );
    $sms_amount = $no_pg_sms;
    $query = "SELECT * FROM sms_history WHERE user_id=".$user_id."";
    $result = mysql_query( $query );
    while( $rs = mysql_fetch_array( $result ) ) {
        $remaining = $rs['remaining'] - $sms_amount;
        $spent = $rs['spent'] + $sms_amount;
    }
    $qupdate = "UPDATE sms_history SET `remaining` = ".$remaining.", `spent` = ".$spent." WHERE user_id = ".$user_id."";
    mysql_query( $qupdate );    
}

I tried sending a text messaging containing this:

Dear customer,we apologise for issues experienced while logging in last wk,follow the link below to reset ur password http://bit.ly/i5ud1zhm,www.boltsns238.com

It charged me 2 credits instead of one credit. I need help with any PHP function to trim, escape, or solve the problem.

rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
  • 3
    http://stackoverflow.com/questions/8349831/best-way-to-detect-number-of-sms-needed-to-send-a-text – Glavić Aug 14 '13 at 08:27
  • 2
    `strlen` returns the size of the input in *bytes*, not in *characters*, so this code looks like it would have problems -- even though it probably should not with your example input. – Jon Aug 14 '13 at 08:34
  • I have modified my code and posted below – plain jane Aug 14 '13 at 09:40
  • Thanks you all, tnks @Sonali for your editing,you are awesome, actually i notice strlen returns in bytes for ASCII characters, so i used `utf8_decode()` function.i.e `strlen(utf8_decode($string))`. Do you guys think it will resolve the issue? – Nzelum Mayor Chukwuebuka Aug 14 '13 at 22:16
  • Look into [`mb_strlen`](http://ca.php.net/manual/en/function.mb-strlen.php). – rink.attendant.6 Aug 15 '13 at 14:59
  • edited the function with mb_strlen and posted the code – plain jane Aug 16 '13 at 04:14

2 Answers2

0

I have modified your function

function deduct_sms_credit( $user_id, $msg ) {
     $per_sms = 160;
     $no_pg_sms = 0;

     $len = mb_strlen($msg);

     if($len<=160)
     {
        $no_pg_sms = 0;
     }
     else if ($len % $per_sms == 0) {
         $no_pg_sms = $len / $per_sms;
      } else {
          $str_sms = chunk_split($msg,160,"|*|");
          $arr_sms = explode("|*|", $str_sms);
          foreach($arr_sms as $key=>$value)
          {
             if(mb_strlen($value) !== 0)
                 $no_pg_sms++;
          }
    }

    $sms_amount = $no_pg_sms;
    $query = "SELECT * FROM sms_history WHERE user_id=".$user_id."";
    $result = mysql_query( $query );
    while( $rs = mysql_fetch_array( $result ) ) {
      $remaining = $rs['remaining'] - $sms_amount;
      $spent = $rs['spent'] + $sms_amount;
  }
  $qupdate = "UPDATE sms_history SET `remaining` = ".$remaining.", `spent` = ".$spent."   WHERE user_id = ".$user_id."";
  mysql_query( $qupdate );    
  }

Please try this and let me know

plain jane
  • 1,009
  • 1
  • 8
  • 19
  • its still not working at all, still deducts 2 SMS credits, try it with this text **Dear user,ve u tested our service?pls if u r xperiencing problem loggin in let us knw,buy more sms credits online,call/text to 07032973337, www.boltsms247.com** – Nzelum Mayor Chukwuebuka Aug 24 '13 at 13:32
  • actually at my end am just calculating the $no_pg_sms which turns out to be 0 in above string example...which amount you are calculating? I think we are not on the same page.. – plain jane Aug 26 '13 at 04:34
  • i have resolved the problem, actually it was my fault, i was passing urlencoded string as $msg to the function which has already added extra characters to the original message – Nzelum Mayor Chukwuebuka Sep 03 '13 at 19:52
-1

Maybe you should try the function mb_strlen

http://ca3.php.net/manual/en/function.mb-strlen.php

The possible issue is that you are passing $msg which is a unicode string or some other multi-byte encoding (eg utf-8). In that case, strlen will return 2 for every 1 character or even more, inflating your strlen.

//=== function to deduct credit per sms

function deduct_sms_credit( $user_id, $msg ) {
    $per_sms = 160;

    //calculate the no. Of sms pages with ceil() function
    $no_pg_sms = ceil( mb_strlen( $msg ) / $per_sms );
    $sms_amount = $no_pg_sms;
    $query = "SELECT * FROM sms_history WHERE user_id=".$user_id."";
    $result = mysql_query( $query );
    while( $rs = mysql_fetch_array( $result ) ) {
        $remaining = $rs['remaining'] - $sms_amount;
        $spent = $rs['spent'] + $sms_amount;
    }
    $qupdate = "UPDATE sms_history SET `remaining` = ".$remaining.", `spent` = ".$spent." WHERE user_id = ".$user_id."";
    mysql_query( $qupdate );    
}
beiller
  • 3,105
  • 1
  • 11
  • 19