CodeIgniter Forums

Full Version: Split String and Number in PHP
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello guys, i have a text field where a user enters their name and number, and i want to insert the number only, how do i achieve this in my controller?The text field is called recipients and the database column is called receiver, I tried a string explode but it gave me an error: Undefined Offset [1]: Here is the code:
Code:
<?php defined( 'BASEPATH' ) OR die('No direct script access allowed!');

class sms extends CI_Controller {

   public function __construct() {
       parent::__construct();
       $this->config->load('facebook');
       $this->load->library('form_validation');
       $this->load->library('encrypt');
       $this->load->model('countries');
       $this->load->helper('text');
       $this->load->model('sms_model');
   }
       public function index() {
   }

   public function sms_form(){


       $sender = ('sender');
       $body = $this->input->post('body');
       $numberofpages = ceil(strlen($body)/160);
       $sendscheduletime = '';
           if(isset($_POST['datepicker'])){
               $sendscheduletime .= $_POST['datepicker'];
           }
           if(isset($_POST['timepicker'])){

               $sendscheduletime .= ' ' .$_POST['timepicker'];
           };
       $route = $this->input->post('route');
       $category = $this->input->post('category');
       $recepientscount = $this->input->post('recepientscount');
       $recepients = $this->input->post('recepients');
       $contacts = explode(" ", $recepients);
       $receiver = $contacts[1];

       $sumcharge = $this->input->post('sumcharge');
       $sentstatus = $this->input->post('sentstatus');

       $sms_data = array(
           'body' => $body,
           'numberofpages' => $numberofpages,
           'sender' => 'inclusion',
           'sendscheduletime' => $sendscheduletime,
           'route' => $route,
           'category' => $category,
           'recepientscount' => $recepientscount,
           'receiver' => $receiver,
           'sumcharge' => $sumcharge,
           'sentstatus' => $sentstatus,
       );
       var_dump($sms_data);die;
        $this->sms_model->insert($sms_data);
        echo "Message Send Successfully....!!!!";
       // $this->load->view('modal_send_sms'); // Reloading after submit.
       // $this->redirect('outbox');
   }
}

/* End of file sms.php */
Why don't you just do two fields, one for name and one for number.

I cannot see an explode function in your code.

After explode, you can use count to check the elements in the array or isset to see if the array element you expected exists or not and deal with the error however you wish.

Paul
(03-02-2018, 04:31 AM)PaulD Wrote: [ -> ]Why don't you just do two fields, one for name and one for number.

I cannot see an explode function in your code.

After explode, you can use count to check the elements in the array or isset to see if the array element you expected exists or not and deal with the error however you wish.

Paul

Oh thats okay, the issue is i want to store the number only
Best way is by using a regular expression.

Example:
PHP Code:
$str 'Johnson 54321';

if ( 
preg_match '/\D+([0-9]+)/'$str$matches ) )
{
 
   print_r($matches);

The pattern \D+([0-9]+) says: check for 1 or more non-digit characters first, followed by 1 or more digit characters.
The part that is between braces, will be returned in the $mathes array.

The result will be in $matches[1], which will be equal to '54321'.