Connecting Tech Pros Worldwide Help | Site Map

Is there a php command to take a specific field from a delimited line of text?

  #1  
Old July 17th, 2005, 02:36 AM
tino@tino.com
Guest
 
Posts: n/a
Hi,

I have a string which is multiple fields delimited by commas.

I would like a php command that says... take the 3rd field for
example.

ie. If MYVARIABLE has the data "hello,there,big,world".

I would like to be able to say, pickup the 3rd field "big" without
knowing the character positions or lengths.

Any help would be much appreciated.

Thanks.

Tino
  #2  
Old July 17th, 2005, 02:36 AM
Senator Jay Billington Bulworth
Guest
 
Posts: n/a

re: Is there a php command to take a specific field from a delimited line of text?


In article <3fd8eff2.5231537@news.clari.net.au>, tino@tino.com wrote:
[color=blue]
> ie. If MYVARIABLE has the data "hello,there,big,world".
>
> I would like to be able to say, pickup the 3rd field "big" without
> knowing the character positions or lengths.[/color]

<?php
$MYVARIABLE = 'hello,there,big,world';

#Method 1
$fields = explode(',', $MYVARIABLE);
//3rd field is now in $fields[2];

#Method 2
$tok = strtok($MYVARIABLE, ',');
$count = 0;
while($tok){
$count++;
if($count == 3){
$thirdfield = $tok;
break;
}
$tok = strtok(',');
}
?>

hth

--
Bulworth : funha@fung.arg | My email address is ROT13 encoded, decode to mail
--------------------------|--------------------------------------------------
<http://www.phplabs.com/> | PHP scripts and thousands of webmaster resources!
  #3  
Old July 17th, 2005, 02:36 AM
Tino@tino.com
Guest
 
Posts: n/a

re: Is there a php command to take a specific field from a delimited line of text?


thanks!


On Thu, 11 Dec 2003 22:57:38 GMT, Senator Jay Billington Bulworth
<funha@fung.arg> wrote:
[color=blue]
>In article <3fd8eff2.5231537@news.clari.net.au>, tino@tino.com wrote:
>[color=green]
>> ie. If MYVARIABLE has the data "hello,there,big,world".
>>
>> I would like to be able to say, pickup the 3rd field "big" without
>> knowing the character positions or lengths.[/color]
>
><?php
>$MYVARIABLE = 'hello,there,big,world';
>
>#Method 1
>$fields = explode(',', $MYVARIABLE);
>//3rd field is now in $fields[2];
>
>#Method 2
>$tok = strtok($MYVARIABLE, ',');
>$count = 0;
>while($tok){
> $count++;
> if($count == 3){
> $thirdfield = $tok;
> break;
> }
> $tok = strtok(',');
>}
>?>
>
>hth
>
>--
>Bulworth : funha@fung.arg | My email address is ROT13 encoded, decode to mail
>--------------------------|--------------------------------------------------
><http://www.phplabs.com/> | PHP scripts and thousands of webmaster resources![/color]

Closed Thread