Split a 16-digit number in PHP

Asked

Viewed 865 times

9

Number: 9999999999999 Stay like this: 9999 9999 9999 9999

$num1 = "9999"; //parte 1
$num2 = "9999"; //parte 2
$num3 = "9999"; //parte 3
$num4 = "9999"; //parte 4

I would like to do this without losing any number or adding anything, just leaving in 4 parts in the order that was.

4 answers

12

You can break this string into equal parts with the function str_split() that returns an array. It is paired with explode() but instead of breaking the string by a delimiter str_split() does the same only, based on a fixed size.

$arr = str_split('9999999999999999', 4);

echo "<pre>";
print_r($arr);

Exit:

Array
(
    [0] => 9999
    [1] => 9999
    [2] => 9999
    [3] => 9999
)

Another way to separate and format this string is with chunk_split() that adds the(s) character(s) to each interval, note that at the end of the string also an underline can be removed with rtirm() and specify the character you want to remove.

echo chunk_split('9999999999999999', 4, '_');
echo rtrim(chunk_split('9999999999999999', 4, '_'), '_');

Exit:

9999_9999_9999_9999_
9999_9999_9999_9999
  • 1

    I don’t quite understand.

  • 1

    The purpose of this function is to break them, correct? str_split('9999999999999', 4); Mas and the function of picking the distributed number? Just like you said. $num1 = "9999"; //part 1 $num2 = "9999"; /part 2 $num3 = "9999"; //part 3 $num4 = "9999"; /part 4

  • 1

    @Wolfprog. do you want to add or split the number? or just add spaces every 4 characters?

  • See the documentation for str_split(), it is very interactive... but this function even that will help you solve your problem... it will turn your number into "arrays"

  • 1

    Darley, below, already solved my problem. But it still helped a lot in the ethics of the resolution.

  • 1

    You’ll split the numbers and print them in any way you want on the front end... if you want to play it that way in the database... you’ll have to add an empty space between the numbers... This is actually a good thing to do in javascript, it’s more of a front-end taskend... if your doubt is the same... print on the screen!

Show 1 more comment

8

Based on the @rray response

$separado = implode(' ', str_split('9999999999999999', 4));

If I wanted to score:

$separado = implode('.', str_split('9999999999999999', 4));

As explained above, the split splits the string into equal parts. Additionally the implode "glue" the separate pieces, using a string at your choice among them.

If extra space at the end is not a problem, see more elegant alternative with chunk_split in the reply mentioned.

If the number of digits varies and you want something like

99 9999 9999

can use this alternative:

for($i=strlen($string); $i>0; $i-=4) $string=substr_replace($string, ' ', $i, 0);

It inserts the "end to start" spaces. See working on IDEONE.

7

Will it always be 16 digits?? You can solve it in a very simple way

$str = "9999999999999999";
$num1 = substr($str, 0, 4);
$num2 = substr($str, 4, 4);
$num3 = substr($str, 8, 4);
$num4 = substr($str, 12, 4);

Read more in the Documentation

3

If you take into account the monetary decimal home score, you will have to reverse the value of the string in order to score correctly and then reverse to have the value of the original string scored.

var_dump(formater("1234567891234567",5,"."));

function formater($str_number, $quantity, $char){
    $arr_number_reverse = array_reverse(str_split($str_number));
    $separado = implode($char, str_split(implode($arr_number_reverse), $quantity));
    return implode(array_reverse(str_split($separado)));
}

**Do the test, change the amount of house to be applied the character chosen...

Based on the solution of Bacchus.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.