Dragosvr92 Posted September 30, 2012 Share Posted September 30, 2012 I am looking into splitting a number of this format "1234567890024" in an array that would look like this: Array ( [0] => 1 [1] => 23 [2] => 45 [3] => 67 [4] => 89 [5] => 002 [6] => 4 ) I had a look at str_split, but it doesnt seem to allow you to split a number in different numbers.. Quote Link to comment Share on other sites More sharing options...
spfoonnewb Posted September 30, 2012 Share Posted September 30, 2012 (edited) Assuming the number is always the same length and format, you can use regex. Below is one example: <?php $input = '1234567890024'; preg_match('/([0-9]{1})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{3})([0-9]{1})/', $input, $match); if (isset($match[0]) && $match[0] == $input) unset($match[0]); print_r($match); Edited September 30, 2012 by spfoonnewb Quote Link to comment Share on other sites More sharing options...
Christian F. Posted September 30, 2012 Share Posted September 30, 2012 (edited) A bit cleaner version, as you only need to use \d to check for digits. <?php $input = '1234567890024'; preg_match('/(\\d)(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{3})(\\d)/', $input, $match); if (isset($match[0])) { // If index 0 is set it will always match the entire RegExp. unset($match[0]); } print_r($match); Edited September 30, 2012 by Christian F. Quote Link to comment Share on other sites More sharing options...
Dragosvr92 Posted September 30, 2012 Author Share Posted September 30, 2012 Thank you both. I will go for the cleaner version's preg match, and use the shorter if statement. Is there any documentation of the preg_match things used to split something? I dont know how they work at all. Also, do you know how may i replace $match[5] aka 89 with a word that is found inside another array of words? The word inside the array would have the number 89. Should i use an if statement, or some str/preg_replace ....? Quote Link to comment Share on other sites More sharing options...
Christian F. Posted September 30, 2012 Share Posted September 30, 2012 The PHP manual contains all the information you need about the different PHP functions. As for your second question: If that word has the index "89" in the array, then it's just to use $match[5] to reference it. Like this: $letter = $letterArray[$match[5]]; You can even use the same method to overwrite the match, by simply substituting the new variable with $match[5]. Quote Link to comment Share on other sites More sharing options...
Dragosvr92 Posted September 30, 2012 Author Share Posted September 30, 2012 I dont understand.... So i have this array : Array ( [1] => 1 [2] => 23 [3] => 45 [4] => 67 [5] => 89 [6] => 002 [7] => 4 ) I need to make an array for each index, and output the matching data. If [1] outputs 1, then it should replace 1 with male, and 2 with female. I would prefer to do this from an array of : Array( [1] => Male [2] => Female ) Something like: <?php $gender = $match[1]; if($gender == "1"){echo"Male";}else{echo"Female";} ?> Do i make any sense? Quote Link to comment Share on other sites More sharing options...
spfoonnewb Posted September 30, 2012 Share Posted September 30, 2012 There are many ways to do this, however based on the above example, it would be the following: <?php $input = '1234567890024'; preg_match('/(\\d)(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{3})(\\d)/', $input, $match); if (isset($match[0])) { // If index 0 is set it will always match the entire RegExp. unset($match[0]); } $genderArr = array( 1 => 'Male', 2 => 'Female' ); if (isset($genderArr[$match[1]])) echo $genderArr[$match[1]]; var_dump($match); Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 30, 2012 Share Posted September 30, 2012 Why in the world are you doing this? Quote Link to comment Share on other sites More sharing options...
Barand Posted September 30, 2012 Share Posted September 30, 2012 "sans regex" method <?php $n = '1234567890024'; $arr = sscanf($n,'%01s%02s%02s%02s%02s%03s%01s'); echo '<pre>'.print_r($arr, 1).'</pre>'; ?> result Array ( [0] => 1 [1] => 23 [2] => 45 [3] => 67 [4] => 89 [5] => 002 [6] => 4 ) Quote Link to comment Share on other sites More sharing options...
Dragosvr92 Posted September 30, 2012 Author Share Posted September 30, 2012 Because that long number means something, and i need to extract the data in it. Thank you spfoonnewb. Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 30, 2012 Share Posted September 30, 2012 Well duh. Why would you store data in a format like that is the question? Quote Link to comment Share on other sites More sharing options...
Dragosvr92 Posted October 1, 2012 Author Share Posted October 1, 2012 (edited) Well duh. Why would you store data in a format like that is the question? Because that number is in an Romanian Social Security Number format, and i wanted to extract all the data out of it, for personal use. I finished the script when i got the Age script from the previous topic i posted in. Edited October 1, 2012 by TheKiller Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted October 1, 2012 Share Posted October 1, 2012 Is this solved yet? Quote Link to comment Share on other sites More sharing options...
Dragosvr92 Posted October 2, 2012 Author Share Posted October 2, 2012 Yes, it is solved. I wasnt used with the new forum software, and havent noticed the solved button. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.