2

How to show data from my database, but with space in it? I have data "6285398980808" on my database, but i want to show it like "62 853 9898 0808". Any suggestion greatly appreciated.

8
  • what's your straucture for seprate? Commented Feb 2, 2016 at 6:16
  • Is all data which you want is in 6285398980808 format? Commented Feb 2, 2016 at 6:18
  • @paranoid what do you mean? If You mean that what I've worked, here some my code $b="select * from tb_dinilai"; $display=mysql_query($b); $row=mysql_fetch_array($display); $nip2=$row['nip_dinilai']; echo $nip2; Commented Feb 2, 2016 at 6:21
  • are you seprate all data like this 00 000 0000 0000? Commented Feb 2, 2016 at 6:22
  • @AjayMakwana I want to show it as 62 853 9898 0808 (with space) Commented Feb 2, 2016 at 6:25

4 Answers 4

3

If your number fixed as 13 digits number than you can use preg_replace() as:

<?php
//62 853 9898 0808 required
$number = 6285398980808;

$formatted_number = preg_replace("/^(\d{2})(\d{3})(\d{4})(\d{4})$/", "$1 $2 $3 $4", $number);
echo $formatted_number; // 62 853 9898 0808
?>
Sign up to request clarification or add additional context in comments.

2 Comments

It's working, i can use it for other separate format too
@AndityaPratama: ahan, best of luck bro... happy coding.
2

There is a special function for that number_format - just play with parameters.

1 Comment

The OP's request for formatting is not separated in thousands.
2

You can use number_format():

number_format — Format a number with grouped thousands

1 Comment

The OP's request for formatting is not separated in thousands.
2

Use CONCAT_WS(' ', SUBSTR(str, 1, 2), SUBSTR(str, 3, 3), SUBSTR(str, 6, 4), SUBSTR(str, 10, 4)) in your SELECT statement. Just replace 'str' with the name of the field.

If the field name is nip_dinilai and your table name is tb_dinilaithen the query will look like this :

SELECT CONCAT_WS(' ', SUBSTR(nip_dinilai, 1, 2), SUBSTR(nip_dinilai, 3, 3), SUBSTR(nip_dinilai, 6, 4), SUBSTR(nip_dinilai, 10, 4)) AS nip_dinilai FROM tb_dinilai

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.