Php Numerical format of 000 000 000 0 [duplicate]

0

Good,

I'm creating an input where it sends a code. This code should be entered as well.

 INPUT-TEXT Formato -> 000 000 000 0

What is the best way to do it in this format?

Example, I'll write in input 213213123, and it's like 000 000 000.

    
asked by anonymous 12.08.2018 / 20:46

2 answers

3

For your tag being PHP, you could do:

$text = "1234567890";

echo implode(" ", str_split($text, 3));

Considering that $text the input source of information, such as $_POST ... str_split() will return an array where each value will have 3 bytes (unicode is not supported) , while implode() will add spaces to each array value.

    
12.08.2018 / 20:55
1

You can use the chunk_split function, which is used to divide the string by a character in succession, an example of use is to split a BASE64 into a format compatible with RFC 2045 , for your case are spaces, then you can do so:

<?php

$str = '1234567890';
$str2 = trim( chunk_split($str, 3, ' ') );

var_dump( $str2 );
  

The trim is to remove the possible extra spaces at the end.

Other suggestions that you can adapt for you are:

For support for unicode this solution seems to meet:

12.08.2018 / 22:21