By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,384 Members | 714 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,384 IT Pros & Developers. It's quick & easy.

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

P: 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
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
In article <3f**************@news.clari.net.au>, ti**@tino.com wrote:
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.


<?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 : fu***@fung.arg | My email address is ROT13 encoded, decode to mail
--------------------------|--------------------------------------------------
<http://www.phplabs.com/> | PHP scripts and thousands of webmaster resources!
Jul 17 '05 #2

P: n/a
thanks!
On Thu, 11 Dec 2003 22:57:38 GMT, Senator Jay Billington Bulworth
<fu***@fung.arg> wrote:
In article <3f**************@news.clari.net.au>, ti**@tino.com wrote:
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.


<?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 : fu***@fung.arg | My email address is ROT13 encoded, decode to mail
--------------------------|--------------------------------------------------
<http://www.phplabs.com/> | PHP scripts and thousands of webmaster resources!


Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.