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

Using File() with blank lines

P: n/a
Let me admit right up front this is my first foray into php. I'm trying
to simply use a small data file to populate a page. I'm using the
file() function but I want my "data records" to be seperated by a double
\n.

My problem is I can't seem to compare a line to anything. My first
problem was determining the end of a data block, finding the double
newline. But in frustration, I just tried to compare a line to a known
value and I still can't compare strings correctly.

Here's a sample data file,

-----------
N2325N
Piper Tomahawk

N2378K
Piper Tomahawk

N11242
Cessna 150

N68440
Cessna 152

----------------------

btw, I even tried '*' chars between lines and couldn't match them
correctly.

Here's what I'm trying to do,
function getNextDataBlock() {
global $data;
$blk = array();

$line = array_shift($data);
while ($line <> "") {
array_push($blk, $line);
$line = array_shift($data);
}

return $blk;
}

$dataBlock = getNextDataBlock();
echo $dataBlock;

Here's what it's become,

function getNextDataBlock() {
global $data;
$blk = array();
//echo "<br />data ==>"; print_r($data);
//echo "<br />blk ==>"; print_r($blk);

$line = array_shift($data);
while ($line <> "") {
echo "<br />+$line";
if ($line == "") print("nothing");
if ($line == " ") print("space");
if ($line == "*") print("star");
if ($line == "N2378K") print("tailnumber");

array_push($blk, $line);
//echo "<br />blk ==>"; print_r($blk);

$line = array_shift($data);
rtrim($line);
echo "<br /><br />getting new line ==> [$line]";
}

//echo '<br />looking in blk ==>'; print_r($blk);
return $blk;
}

//$data[3] = "";
$dataBlock = getNextDataBlock();
echo $dataBlock;
Even when I know a line, like trying to match a known tailnumber,
N2378K, the line doesn't fire!
Thanks for any help. I'm open to completely new ideas on how to read in
this small data file and strip off one block of data at a time but I'd
also like to understand why I can't find newlines. I've tried
everything, comparing for "\n", x32 actually show up on the web page so
I tried searching for a space, nothing. I can't kill the loop on an
empty line.

Many thanks,

/jim

Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
NC
Spin wrote:

Let me admit right up front this is my first foray into php.
I'm trying to simply use a small data file to populate a page.
I'm using the file() function but I want my "data records"
to be seperated by a double \n.

Here's a sample data file,

-----------
N2325N
Piper Tomahawk

N2378K
Piper Tomahawk

N11242
Cessna 150

N68440
Cessna 152

----------------------


Try this:

$data = file('filename.txt');
$i = 0;
foreach ($data as $line) {
$line = trim($line);
if (strlen($line) == 0) {
$i = 0;
continue;
} else {
$i++;
}
switch ($i) {
case 1:
echo "Tail number: $line \r\n";
break;
case 2:
echo "Airplane model: $line \r\n";
break;
default:
continue;
}
}

Cheers,
NC

Jul 17 '05 #2

P: n/a
On 4 Jun 2005 20:45:51 -0700, "NC" <nc@iname.com> wrote:
foreach ($data as $line) {
$line = trim($line);


Thanks NC, that give me something to think about. As a C/C++/OP
programmer for 15 yrs now I struggle with language constructions like
($data as $line) so I'll have to think on this method a bit.

I finally found that "\r\n" was the correct end of line sequence. The
trouble however, is that even if I rtrim() the line, I still need to use
this as the indicator. Does that mean that rtrim() isn't properly
removing them or am I missing something? I don't know but I'm up and
running so for now that's water under the bridge.

Just to summarize what I ended up with

$page = array();
$file = 'fleet.tdb';
$data = file($file) or die('Could not read file!');
$blk = array();

function getNextDataBlockEndIndex() {
global $data;
$stopmark = "\r\n";

$i = -1;
do {
if (++$i < count($data)) $line = $data[$i];
//rtrim($line); // makes no difference
} while (($i < count($data)) AND (!(strcmp($line, $stopmark)
== 0)));
return $i;
}

function getNextDataBlock() {
global $blk;
global $data;

$idx = getNextDataBlockEndIndex();
$blk = array_slice($data, 0, $idx);
reset($data);
$data = array_slice($data, $idx+1);
}

function getNextPlaneBlock() {
global $blk;
getNextDataBlock();
if (! empty($blk)) {
echo "<div class=\"ad_note\">";
echo "<div class=\"fleet_tn\"><a target=\"pic_view\"
href=\"images/fleet/$blk[1]\">
<img src=\"images/fleet/$blk[2]\" border=\"0\"
alt=\"thumbnail\" /></a></div>";
echo "<p class=\"fleet_text\" />";
echo "$blk[3], $blk[0]<br />$blk[4]<br />$blk[5]<br />$blk[6]
<br />$blk[7]<br />";
echo "<br />&nbsp;";
echo "</div>";
return 1;
} else return 0;
}
Then later within the body of the document all I need is,

<!-- Fleet box -->
<center>
<div class="fleet_box">

<!-- Plane -->
<?php do ; while (getNextPlaneBlock() == 1);?>

</div>
</center>
And I have a completely dynamic listing of the entire fleet managed by a
simple text file with no further intervention required.

Now, I do have a slightly expanded data file, the original was simply an
example... but more importantly easily expanded in the future and if new
data needs to be added I simply adjust the use of the $blk[x] elements
in the output function getNextPlaneBlock(), one time only.

N2325N
f01_n2325n.jpg
tn_f01_n2325n.jpg
Piper Tomahawk
2-seater, 110 hp
data 3
data 4
$54 / hr (Wet)

N2378K
f02_n2378k.jpg
tn_f02_n2378k.jpg
Piper Tomahawk
2-seater, 110 hp
data 3
data 4
$54 / hr (Wet)

N11242
f10_n11242.jpg
tn_f10_n11242.jpg
Cessna 150
2-seater, 100 hp
data 3
data 4
$54 / hr (Wet)
If anyone has any ideas or comments on something I've done wrong,
inefficiently or how to simply make the method better, I'm ALL ears.
Thanks for the input...
/jim/houston/tx


Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.