Jump to content

Split Number Into Different Characters


Dragosvr92

Recommended Posts

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..

Link to comment
Share on other sites

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 by spfoonnewb
Link to comment
Share on other sites

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 by Christian F.
Link to comment
Share on other sites

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 ....?

Link to comment
Share on other sites

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].

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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 by TheKiller
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.