Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Need Help in Message Length Function
#1

[eluser]Ahmed Iqbal[/eluser]
Hello Dear Friends,

Guys I need your help in one function. Actually I have built one function for sms related site. As you know mostly sms text message page contain '160' characters so I want to represt '1 length' for this. When sms character greater then 160 so my sms length 'equal to 2'. bla bla.
So this is function.

Code:
function sms_length($value) {
    
    if ($value <= 160)              {
        return 1;
    }
    elseif ($value <= 320)       {
        return 2;
    }
    elseif ($value <= 480)       {
        return 3;
    }
    elseif ($value <= 640)          {
        return 4;
    }
}

Controller:
Code:
// Loading Message Lenght Helper
$this->load->helper('length');

$form_data = array(
'cat_id'=> $this->input->post('category'),
'sms_title'=> $this->input->post('title'),
'sms_desc'=> $this->input->post('desc'),
'sms_char'=> strlen($this->input->post('desc')),
'sms_length'=> sms_length($this->input->post('desc')),
);

// After that, All result inserted in to DB


But know, when I want to built one helper for my codeigter and want to get result from this so every time I got ‘1 length’ from this…!
Guys I need your help to built more solid and power full logic for this function and please help me to get proper result from this function.

thanks and waiting for your reply... :p
#2

[eluser]Siddhesh[/eluser]
Hi Ahmed Iqbal,

You need to use strlen function to get the length of the string instead checking against the string Or pass the the length of the string instead of passing the actual text.



$form_data = array(
'cat_id'=> $this->input->post('category'),
'sms_title'=> $this->input->post('title'),
'sms_desc'=> $this->input->post('desc'),
'sms_char'=> strlen($this->input->post('desc')),
'sms_length'=> sms_length(strlen($this->input->post('desc'))),
);

Must work for you.

Thanks.
#3

[eluser]Ahmed Iqbal[/eluser]
Thanks Siddhesh, You are right...!
What the silly mistake...! Sad
#4

[eluser]InsiteFX[/eluser]
Another option would be to use the CodeIgniter text_helper

CodeIgniter User Guide - Text Helper

character_limiter()
Code:
$string = "Here is a nice text string consisting of eleven words.";

$string = character_limiter($string, 20);

// Returns: Here is a nice text string…

InsiteFX
#5

[eluser]danmontgomery[/eluser]
Code:
function sms_length($value) { return ceil(strlen($value)/160); }

or just directly split the string by length

Code:
function sms_split($value) { return str_split($value, 160); }




Theme © iAndrew 2016 - Forum software by © MyBB