|
 |
beginning_php thread: Reading/writing to a delimited file
Message #1 by "Mike Barber" <chanyom@h...> on Wed, 10 Jan 2001 08:53:33 -0000
|
|
I am sure this must be simple, and that it is just going over my
head...but how do I do this:
I want to be able to read and write to a delimited text file that looks
something like this:
Jim|Blanchard|jim@h...|555-5555
Mike|Richards|mike@h...|777-7777
etc...
I would be using variables for each field ($firstName, $lastName, etc).
I have a handle on the example in Ch.10, but it only covers using
fseek()function...nothing using delimiters.
Can anyone help this poor newbie?
---
NEED TECHNICAL TIPS, TOOLS, AND INSIGHTS? Is FREE okay?
Visit EarthWeb for the latest in IT Management, Software Development,
Web Development, Networking & Communications, and Hardware & Systems.
Click on http://www.earthweb.com for FREE articles, tutorials,
and discussions from the experts.
---
You are currently subscribed to beginning_php as: $subst('Recip.EmailAddr')
To unsubscribe send a blank email to leave-beginning_php-$subst('Recip.MemberIDChar')@p2p.wrox.com
Message #2 by <gaurav?khanna25@y...> on Fri, 12 Jan 2001 15:05:19 +0530
|
|
hey Mike i tried using this by 2 way
well just use a for loop conditn & chk for the de-limiter if u r keeping
or 2nd way is use RegEx ie Regular Expressions to solve it
well i give u a simple code for the ( for loop conditn )
well i also new to PHP so plz chk others egs. too .
well i tried my method the best way i can do may be a bit lengthy too ..
Ist Method using for loop Conditn
just execute ( cut & paste )
$var contains a string ""
<html>
<body bgcolor="blue" text="white">
<?php
/*
Remember this is an eg. to ur soln, pl note the delimiter u use should also
end at the string too
i used delimiter as "$" u can replace it with ur own too say "
*/
$var = "Gautam-Khanna|India-(Mumbai)|gaurav_khanna25@y...|555-5555|";
$len = strlen($var);
echo "file $var <br> length $len ";
$ctr=0;
for($i=0;$i<5;$i++)
{
if($i==0) {
while($var[$ctr]!="|")
{
$name = $name.$var[$ctr] ;
$ctr++;
}
echo "<br> name : $name "; $ctr++; $i++;
}
if($i==1) {
while($var[$ctr]!="|")
{
$addr = $addr.$var[$ctr] ;
$ctr++;
}
echo "<br> address : $addr "; $ctr++; $i++;
}
if($i==2) {
while($var[$ctr]!="|")
{
$email = $email.$var[$ctr] ;
$ctr++;
}
echo "<br> Email : $email "; $ctr++; $i++;
}
if($i==3) {
while($var[$ctr]!="|")
{
$phone = "".$phone.$var[$ctr] ;
$ctr++;
}
echo "<br> Phone : $phone "; $ctr++; $i=4; // used $ i as to end the
result
}
}
?>
</body>
</html>
****************** output of the prgm is **********************
file Gautam-Khanna|India-(Mumbai)|gaurav_khanna25@y...|555-5555|
length 64
name : Gautam-Khanna
address : India-(Mumbai)
Email : gaurav_khanna25@y...
Phone : 555-5555
Regards Gautam
mail me for any suggestions of urs (or just say u liked the script that
would plz me bye haha)
gaurav_khanna25@y...
-----Original Message-----
From: Mike Barber <chanyom@h...>
To: beginning php <beginning_php@p...>
Date: Wednesday, January 10, 2001 4:37 PM
Subject: [beginning_php] Reading/writing to a delimited file
>I am sure this must be simple, and that it is just going over my
>head...but how do I do this:
>
>I want to be able to read and write to a delimited text file that looks
>something like this:
>
>Jim|Blanchard|jim@h...|555-5555
>Mike|Richards|mike@h...|777-7777
>etc...
>
>I would be using variables for each field ($firstName, $lastName, etc).
>
>I have a handle on the example in Ch.10, but it only covers using
>fseek()function...nothing using delimiters.
>
>Can anyone help this poor newbie?
>
Message #3 by <gaurav?khanna25@y...> on Fri, 12 Jan 2001 15:41:33 +0530
|
|
hey Mike i tried using this by 2 way
well just use a for loop conditn & chk for the de-limiter if u r keeping
or 2nd way is use RegEx ie Regular Expressions to solve it
well i give u a simple code for the ( for loop conditn )
well i also new to PHP so plz chk others egs. too .
well i tried my method the best way i can do may be a bit lengthy too ..
Ist Method using for loop Conditn
just execute ( cut & paste )
$var contains a string ""
<html>
<body bgcolor="blue" text="white">
<?php
/*
Remember this is an eg. to ur soln, pl note the delimiter u use should also
end at the string too
i used delimiter as "$" u can replace it with ur own too say "
*/
$var = "Gautam-Khanna|India-(Mumbai)|gaurav_khanna25@y...|555-5555|";
$len = strlen($var);
echo "file $var <br> length $len ";
$ctr=0;
for($i=0;$i<5;$i++)
{
if($i==0) {
while($var[$ctr]!="|")
{
$name = $name.$var[$ctr] ;
$ctr++;
}
echo "<br> name : $name "; $ctr++; $i++;
}
if($i==1) {
while($var[$ctr]!="|")
{
$addr = $addr.$var[$ctr] ;
$ctr++;
}
echo "<br> address : $addr "; $ctr++; $i++;
}
if($i==2) {
while($var[$ctr]!="|")
{
$email = $email.$var[$ctr] ;
$ctr++;
}
echo "<br> Email : $email "; $ctr++; $i++;
}
if($i==3) {
while($var[$ctr]!="|")
{
$phone = "".$phone.$var[$ctr] ;
$ctr++;
}
echo "<br> Phone : $phone "; $ctr++; $i=4; // used $ i as to end the
result
}
}
?>
</body>
</html>
****************** output of the prgm is **********************
file Gautam-Khanna|India-(Mumbai)|gaurav_khanna25@y...|555-5555|
length 64
name : Gautam-Khanna
address : India-(Mumbai)
Email : gaurav_khanna25@y...
Phone : 555-5555
Regards Gautam
mail me for any suggestions of urs (or just say u liked the script that
would plz me bye haha)
gaurav_khanna25@y...
-----Original Message-----
From: Mike Barber <chanyom@h...>
To: beginning php <beginning_php@p...>
Date: Wednesday, January 10, 2001 4:37 PM
Subject: [beginning_php] Reading/writing to a delimited file
>I am sure this must be simple, and that it is just going over my
>head...but how do I do this:
>
>I want to be able to read and write to a delimited text file that looks
>something like this:
>
>Jim|Blanchard|jim@h...|555-5555
>Mike|Richards|mike@h...|777-7777
>etc...
>
>I would be using variables for each field ($firstName, $lastName, etc).
>
>I have a handle on the example in Ch.10, but it only covers using
>fseek()function...nothing using delimiters.
>
>Can anyone help this poor newbie?
Message #4 by "Allan Kent" <allan@l...> on Tue, 16 Jan 2001 05:52:06 -0000
|
|
hi mike,
> I want to be able to read and write to a delimited text file that looks
> something like this:
>
> Jim|Blanchard|jim@h...|555-5555
> Mike|Richards|mike@h...|777-7777
> etc...
your easiest option would be to use the explode() function:
$fp = fopen ("inputfile.txt", "r");
while (!feof ($fp)) {
$buffer = fgets($fp, 4096);
$fields = explode("|", $buffer);
// $fields is now an array
// $fields[0] = "Jim"
// $fields[1] = "Blanchard"
// etc
// proccess your fields here
}
fclose ($fp);
> I would be using variables for each field ($firstName, $lastName, etc).
ok, if you want to slam each of the fields into their own variable you could use the split() function:
$fp = fopen ("inputfile.txt", "r");
while (!feof ($fp)) {
$buffer = fgets($fd, 4096);
list ($fname, $lname, $email, $phone) = split ("\|", $buffer);
// The reason for using \| as the delimiter is that split() uses regex
// so you need to escape the | character
}
fclose ($fp);
hope that helps
allan
|
 |