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.
4 Answers
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
?>
2 Comments
Anditya Pratama
It's working, i can use it for other separate format too
devpro
@AndityaPratama: ahan, best of luck bro... happy coding.
1 Comment
Edrean Ernst
The OP's request for formatting is not separated in thousands.
1 Comment
Edrean Ernst
The OP's request for formatting is not separated in thousands.
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
6285398980808format?tb_dinilai"; $display=mysql_query($b); $row=mysql_fetch_array($display); $nip2=$row['nip_dinilai']; echo $nip2;