Connecting Tech Pros Worldwide Help | Site Map

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

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 01:36 AM
tino@tino.com
Guest
 
Posts: n/a
Default Is there a php command to take a specific field from a delimited line of text?

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, 01:36 AM
Senator Jay Billington Bulworth
Guest
 
Posts: n/a
Default 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, 01:36 AM
Tino@tino.com
Guest
 
Posts: n/a
Default 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]

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.