I have been trying to figure this one out, and so far have come up empty... what I want to do is parse a text file, I am working with this file by using fopen to open it, and then assigning a file pointer to it pulling it in byte by byte. When I get the bytes, I examine them, manipulate some local variables if necessary, and store the chars in their proper containers. When I hit a new line in the input text file, I want to reset all local variables, and use the strings that I have parsed out of the file. This is where I get unexpected results. When I try to print the contents of my arrays, I get the word "Array" for all of them as output. If I print the Arrays by accessing each elements using a loop to print each character, the data is there. I thought this could mean I do not have a null terminator at the ends of my arrays, and because I am using echo to print them out, echo prints out Array, but I don't think this is the case, anyway, here is my code:
first the one that does print:
<?php
// local variable declaration.
$param1 = 1;
$param2 = 0;
$name = array();
$display = array();
/* Grab the conf file that holds the file names and
* their descriptions
*/
$fp = fopen("/home/bdina/public_html/help/howtos.file", "r");
/* Now to move along the file byte-by-byte, and parse
* it...
*/
while(!feof($fp)) {
$char = fgetc($fp);
// detect a newline.
if(ord($char) == 10) {
$param1 = 1;
$param2 = 0;
foreach($name as $val) {
echo ord($val);
}
echo " ";
foreach($display as $val) {
echo $val;
}
echo "<br>";
unset($name);
unset($display);
}
// detect first space.
if((ord($char) == 32) && ($param1 == 1)) {
$param1 = 0;
$param2 = 1;
}
if(($param1 == 1) && ($param2 == 0)) {
$name[] = $char;
}
if(($param1 == 0) && ($param2 == 1)) {
$display[] = $char;
}
}
/* Close it up cause I am done fool. */
fclose($fp);
?>
that code prints the strings out... this one does not...
<?php
// local variable declaration.
$param1 = 1;
$param2 = 0;
$name = array();
$display = array();
/* Grab the conf file that holds the file names and
* their descriptions
*/
$fp = fopen("/home/bdina/public_html/help/howtos.file", "r");
/* Now to move along the file byte-by-byte, and parse
* it...
*/
while(!feof($fp)) {
$char = fgetc($fp);
// detect a newline.
if(ord($char) == 10) {
$param1 = 1;
$param2 = 0;
echo $name . " " . $display . "<br>";
unset($name);
unset($display);
}
// detect first space.
if((ord($char) == 32) && ($param1 == 1)) {
$param1 = 0;
$param2 = 1;
}
if(($param1 == 1) && ($param2 == 0)) {
$name[] = $char;
}
if(($param1 == 0) && ($param2 == 1)) {
$display[] = $char;
}
}
/* Close it up cause I am done fool. */
fclose($fp);
?>
I'm still looking for the answer to this, but if anyone knows it would be appreciated...
thanks,
--Bryan
first the one that does print:
<?php
// local variable declaration.
$param1 = 1;
$param2 = 0;
$name = array();
$display = array();
/* Grab the conf file that holds the file names and
* their descriptions
*/
$fp = fopen("/home/bdina/public_html/help/howtos.file", "r");
/* Now to move along the file byte-by-byte, and parse
* it...
*/
while(!feof($fp)) {
$char = fgetc($fp);
// detect a newline.
if(ord($char) == 10) {
$param1 = 1;
$param2 = 0;
foreach($name as $val) {
echo ord($val);
}
echo " ";
foreach($display as $val) {
echo $val;
}
echo "<br>";
unset($name);
unset($display);
}
// detect first space.
if((ord($char) == 32) && ($param1 == 1)) {
$param1 = 0;
$param2 = 1;
}
if(($param1 == 1) && ($param2 == 0)) {
$name[] = $char;
}
if(($param1 == 0) && ($param2 == 1)) {
$display[] = $char;
}
}
/* Close it up cause I am done fool. */
fclose($fp);
?>
that code prints the strings out... this one does not...
<?php
// local variable declaration.
$param1 = 1;
$param2 = 0;
$name = array();
$display = array();
/* Grab the conf file that holds the file names and
* their descriptions
*/
$fp = fopen("/home/bdina/public_html/help/howtos.file", "r");
/* Now to move along the file byte-by-byte, and parse
* it...
*/
while(!feof($fp)) {
$char = fgetc($fp);
// detect a newline.
if(ord($char) == 10) {
$param1 = 1;
$param2 = 0;
echo $name . " " . $display . "<br>";
unset($name);
unset($display);
}
// detect first space.
if((ord($char) == 32) && ($param1 == 1)) {
$param1 = 0;
$param2 = 1;
}
if(($param1 == 1) && ($param2 == 0)) {
$name[] = $char;
}
if(($param1 == 0) && ($param2 == 1)) {
$display[] = $char;
}
}
/* Close it up cause I am done fool. */
fclose($fp);
?>
I'm still looking for the answer to this, but if anyone knows it would be appreciated...
thanks,
--Bryan