Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string question(s)

Status
Not open for further replies.

bdina

Programmer
Jun 17, 2003
47
0
0
US
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
 
If you want to output the contents of an array, you must use print_r() instead of print.

PHP does not have a default conversion from array type to string for printing.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
so there is no way to just output the array as a string?? I have to use some type of a loop???
 
You might want to look at the implode function.
Code:
$ary = array('a','b','c','d','e');
$str = implode(':',$ary);
echo 'Ary = '.$ary;
would print
Code:
Ary = a:b:c:d:e
You could try
Code:
echo implode('',$name) . " " . implode('',$display) . "<br>";
This is untested, so YMMV

Ken
 
..../
Ok I read this, and read it again, and I'll derail slightly if I may .

1) what are you trying to extract from the file?
2) does this *have* to be done character by character ?
3) when you print out your array, have you tried $your_array[0]; ?
4) is the file a fixed length file?
or definitive standard file (e.g Ansi x12 / as2 etc) ?

Just thinking that maybe theres a different way to approach the beast;
the first way isn't always the right way,
the right way isnt always the easiest way,
the easiest way isnt always the best way.


______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
I don't have to accomplish this this way, I am somewhat working with it more because I am just trying to learn php, and see what it can do for me. I could just store the names of the files in the MySQL db, and also their descriptions, which would be really easy, however I am trying it in a flat file to see how php handles files. I will look at the implode function, however if I can not print strings, then I can not print strings.... I have yet to check out the functions documentation but am wondering if it is like the implode function of SML/NJ.
 
when reading files, normal text or html I normally try to split on whitespace if possible, or a suitable delimiter, also assigning lines a number. heres a quickie example to help you get the idea. (code checked OK)

Code:
<?php
$file="somefile.txt";

if (file_exists($file)){

	$fcontents = file($file);

	while (list ($line_num, $line) = each ($fcontents)) {

		$parts=explode(' ', $line);

		$num_parts=count($parts);

		$i=0;

		while($i < $num_parts){

			echo "$line_num: $parts[$i]<br>";

			$i++;
		}

		echo "<hr>";

	}
}

echo "EOF";

?>

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Do you have to store your strings in variables?

Instead of initializing them as
Code:
   $name = array();
   $display = array();
can't you just initialize them as the null string?
Code:
$name = '';
$display = '';
Then you would do
Code:
      if(($param1 == 1) && ($param2 == 0)) {
         $name .= $char;
      }

      if(($param1 == 0) && ($param2 == 1)) {
         $display .= $char;
      }
The '.=' operator appends whatever is on the right side of the operator to what's on the left side.

Your 'echo' statement would then work as you intended.

Ken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top