![]() |
Split String and Number in PHP - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: Split String and Number in PHP (/showthread.php?tid=70160) |
Split String and Number in PHP - CharlesK - 03-02-2018 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!'); RE: Split String and Number in PHP - PaulD - 03-02-2018 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 RE: Split String and Number in PHP - CharlesK - 03-02-2018 (03-02-2018, 04:31 AM)PaulD Wrote: Why don't you just do two fields, one for name and one for number. Oh thats okay, the issue is i want to store the number only RE: Split String and Number in PHP - Wouter60 - 03-02-2018 Best way is by using a regular expression. Example: PHP Code: $str = 'Johnson 54321'; 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'. |