formating a string

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?

--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
James Savage [ Sa, 12 Mai 2007 21:16 ] [ ID #1710833 ]

Re: formating a string

--------------010003050903000708090402
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

The this instead. BTW, you misunderstood the useage of substr, see
http://uk.php.net/manual/en/function.substr.php.

$code = 'abcdwxyz8765';
$string = array();
array_push($string,substr($code,0,4));
array_push($string, substr($code,4,4));
array_push($string, substr($code,8,4));
echo $string[0].'-'.$string[1].'-'.$string[2];


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?
>
>

--------------010003050903000708090402--
Kevin Smith [ Sa, 12 Mai 2007 21:37 ] [ ID #1710834 ]

Re: formating a string

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

--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
cdmn [ Mo, 13 August 2007 11:27 ] [ ID #1794254 ]
PHP » gmane.comp.php.windows » formating a string

Vorheriges Thema: Wamp/XAMPP and phpmyadmin
Nächstes Thema: ming library