From: "Wei, Alice J." on
Hi,

I have a code here as the following:

<?php

$string="1 0 70.208 61.613 1.424 3.689 61.404 0.000 0.000 0.000 0.000 0.000";

$data= explode("\t", $string);
echo "1: ". $data[0] . " 2: " . $data[1] . " 3: " . $data[2] . " 4: " . $data[3] . " 5: " . $data[4]

?>

When I execute it, I get this error and output:

PHP Notice: Undefined offset: 2 in /root/Desktop/test.php on line 8
PHP Notice: Undefined offset: 3 in /root/Desktop/test.php on line 8
PHP Notice: Undefined offset: 4 in /root/Desktop/test.php on line 8
1: 1 2: 0 70.208 61.613 1.424 3.689 61.404 0.000 0.000 0.000 0.000

Is there some way that I can have it so that it can print out:

1: 1 2:0 3: 70.208 4. 61.613 5. 1.424

I tried using \s for trailing spaces, but It all gave me output of everything in the single line.
Have I missed something here?

Thanks in advance.

Alice
======================================================
Alice Wei
MIS 2009
School of Library and Information Science
Indiana University Bloomington
ajwei(a)indiana.edu
From: "Thiago H. Pojda" on
On Fri, Jul 18, 2008 at 11:41 AM, Wei, Alice J. <ajwei(a)indiana.edu> wrote:

> Hi,



>
> Is there some way that I can have it so that it can print out:
>
> 1: 1 2:0 3: 70.208 4. 61.613 5. 1.424
>
> I tried using \s for trailing spaces, but It all gave me output of
> everything in the single line.
> Have I missed something here?
>

You're just removing every space there is, letting none for values
separation.

Just replace every "\s\s" with "\s" until there's no more "\s\s".

Probably some regex would be better, but that's not my area :)

Regards,
Thiago


>
> Thanks in advance.
>
> Alice
> ======================================================
> Alice Wei
> MIS 2009
> School of Library and Information Science
> Indiana University Bloomington
> ajwei(a)indiana.edu
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


--
Thiago Henrique Pojda
From: Peter Ford on
Thiago H. Pojda wrote:
> On Fri, Jul 18, 2008 at 11:41 AM, Wei, Alice J. <ajwei(a)indiana.edu> wrote:
>
>> Hi,
>
>
>
>> Is there some way that I can have it so that it can print out:
>>
>> 1: 1 2:0 3: 70.208 4. 61.613 5. 1.424
>>
>> I tried using \s for trailing spaces, but It all gave me output of
>> everything in the single line.
>> Have I missed something here?
>>
>
> You're just removing every space there is, letting none for values
> separation.
>
> Just replace every "\s\s" with "\s" until there's no more "\s\s".
>
> Probably some regex would be better, but that's not my area :)
>
> Regards,
> Thiago
>
>
>> Thanks in advance.
>>
>> Alice

You probably need to use a regexp, so you can accomodate the different types of
white space:
<?php

$string="1 0 70.208 61.613 1.424 3.689 61.404
0.000 0.000 0.000 0.000 0.000";

$data = preg_split('/\s+/',$string);
echo "1: ". $data[0] . " 2: " . $data[1] . " 3: " . $data[2] . " 4: " .
$data[3] . " 5: " . $data[4]

?>

Does that work for you?


--
Peter Ford phone: 01580 893333
Developer fax: 01580 893399
Justcroft International Ltd., Staplehurst, Kent
From: "Wei, Alice J." on

======================================================
Alice Wei
MIS 2009
School of Library and Information Science
Indiana University Bloomington
ajwei(a)indiana.edu
________________________________________
From: Peter Ford [pete(a)justcroft.com]
Sent: Friday, July 18, 2008 11:02 AM
To: php-general(a)lists.php.net
Subject: Re: [PHP] Trailing Spaces Problem

Thiago H. Pojda wrote:
> On Fri, Jul 18, 2008 at 11:41 AM, Wei, Alice J. <ajwei(a)indiana.edu> wrote:
>
>> Hi,
>
>
>
>> Is there some way that I can have it so that it can print out:
>>
>> 1: 1 2:0 3: 70.208 4. 61.613 5. 1.424
>>
>> I tried using \s for trailing spaces, but It all gave me output of
>> everything in the single line.
>> Have I missed something here?
>>
>
> You're just removing every space there is, letting none for values
> separation.
>
> Just replace every "\s\s" with "\s" until there's no more "\s\s".
>
> Probably some regex would be better, but that's not my area :)
>
> Regards,
> Thiago
>
>
>> Thanks in advance.
>>
>> Alice

You probably need to use a regexp, so you can accomodate the different types of
white space:
<?php

$string="1 0 70.208 61.613 1.424 3.689 61.404
0.000 0.000 0.000 0.000 0.000";

$data = preg_split('/\s+/',$string);
echo "1: ". $data[0] . " 2: " . $data[1] . " 3: " . $data[2] . " 4: " .
$data[3] . " 5: " . $data[4]

?>

Does that work for you?
Thanks, this worked perfectly!

--
Peter Ford phone: 01580 893333
Developer fax: 01580 893399
Justcroft International Ltd., Staplehurst, Kent

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php