tags:

views:

65

answers:

3

i have a site where users enter a numeric code of 10 numbers. when reading from the db and displaying the codes i want it to display in this format

xxxx-xxxx-xx

how can i do this with php or jquery ?

+2  A: 
$code = "1234567890";
echo substr($code, 0, 4) . "-" . substr($code, 4, 4) . "-" . substr($code, 8, 2);
Marek Karbarz
A: 

You can use Regular Expression.

$Text        = "1234567890";
$Pattern     = '/(.{4})(.{4})(.{2})/';
$Replacement = '$1-$2-$3';
$NewText     = preg_replace($Pattern, $Replacement, $Text);

Hope this helps.

NawaMan
A: 

in jquery, for example:

<script>
    $(document).ready(function(){
        n = $("#IdOfTheElement").text();
        n = n.substr(0, 4) + "-" + n.substr(4, 4) + "-" + n.substr(8);
        $("#IdOfTheElement").text(n)
    }
</script>
Jronny