Php Numeric Format of 000 000 0

Asked

Viewed 211 times

0

Good,

I’m creating an input where it sends a code. This code should be inserted thus.

 INPUT-TEXT Formato -> 000 000 000 0

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

Example, I will write in the input 213213123, and it looks like 000 000 000 0.

2 answers

3

Because your tag is PHP, you could do:

$text = "1234567890";

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

whereas the $text the source of the information, such as a $_POST... The str_split() will return an array where each value will have 3 bytes (Unicode is not supported), as long as the implode() will add spaces in each array value.

1

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

<?php

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

var_dump( $str2 );

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

other suggestions that can adapt for you are in:

For Unicode support this solution seems to suit:

Browser other questions tagged

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