For Programmers: Free Programming Magazines  


Home > Archive > PHP on Windows > August 2007 > Re: formating a string









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: formating a string
cdmn

2007-08-13, 8:01 am

James Savage wrote:
> i have a 12 digit code that i want to format as follows xxxx-xxxx-xxxx it is
> all numbers i tried using substr() to format it in this fassion
> $code = abcdwxyz8765;
> $string = array();
> $string[] = substr($code,1,4);
> $string[] = substr($code,5,8);
> $string[] = substr($code9,12);
> echo $string[0],'-',$string[1],'-',$string[2];
> OUTPUT SHOULD BE : abcd-wxyz-5678
> I GET : -wxyz678-5678
> Any help?


Very very nasty code :/

Here ya go: implode('-', str_split('abcdwxyz8765', 4));
Test:
C:\>php -r "echo implode('-', str_split('abcdwxyz8765', 4));"
abcd-wxyz-8765
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com