PHP File Open and Reading character by character
-
Posted Today, 12:07 AM Hi All Thanks in advance for the help, I have the PHP code below to read contents of the file. //The code does not work, it returns the output below " outputResource id #3 Quote Warning: Invalid argument supplied for foreach() in C:\XAMPP\htdocs\kampcoordinators\test.php on line 10"
-
Posted Today, 12:07 AM Hi All Thanks in advance for the help, I have the PHP code below to read contents of the file. //The code does not work, it returns the output below " outputResource id #3 Quote Warning: Invalid argument supplied for foreach() in C:\XAMPP\htdocs\kampcoordinators\test.php on line 10"
Its how you are handling the variable $output. 1st you assign it a file resource returned from the fopen function. 2nd you use a str_replace on said file resource, not sure why it does not barf here. 3rd the foreach loop requires and array as the first value. ... try replacing the fopen() with file_get_contents() as it will return the entire file contents as a string. Then uncomment the explode which will take the string and replace it with an array. That should allow the foreach to work. always remember to find out what the return type of a function call is before you use it.
-
Its how you are handling the variable $output. 1st you assign it a file resource returned from the fopen function. 2nd you use a str_replace on said file resource, not sure why it does not barf here. 3rd the foreach loop requires and array as the first value. ... try replacing the fopen() with file_get_contents() as it will return the entire file contents as a string. Then uncomment the explode which will take the string and replace it with an array. That should allow the foreach to work. always remember to find out what the return type of a function call is before you use it.
-
Posted Today, 12:07 AM Hi All Thanks in advance for the help, I have the PHP code below to read contents of the file. //The code does not work, it returns the output below " outputResource id #3 Quote Warning: Invalid argument supplied for foreach() in C:\XAMPP\htdocs\kampcoordinators\test.php on line 10"
I think this is what happened. You misused the str_replace() function. If you will check the documentation for it. What you actually did was find an occurrence of "," and replace it with " " and then you searched for "," on your explode(). The character no longer exists by then. Try interchanging the first two arguments of your str_replace(). I think that should do.
-
$output should turn into an array after the explode() right? i think something else is wrong.
the php function defines for reference. --------- fopen function.... resource fopen ( string $filename , string $mode [, bool $use_include_path = false [, resource $context ]] ) --------- explode function.... array explode ( string $delimiter , string $string [, int $limit ] ) --------- foreach looping construct.... foreach (array_expression as $value) statement foreach (array_expression as $key => $value) statement now step through his code....
$output=fopen("testfile.txt","r") or exit("Unable to open file!"); <--- $output is a file resource, this is not the file content.
//(2) Then we can get rid of the"," in there:
$output = str_replace(","," ", $output); <--- he takes the $output which is a file resource and does a string replace on it and re-assigns the variable $output as a string.
//(3) Then we explode it at every ,encountered
//$output = explode(",",$output); <--- when this is uncommented it will change a string into an array. If the source string has no "commas" in it it will return an array with one value. you could also say if(count($output)==1)) {echo "I have 1!";}else{echo "I'm Lost!";}, but currently it does nothing as it is commented out so $output should still be a string.
echo "output"; <--- echos a string called "output"
echo $output; <--- echoes a variable currently a string and most likely just the name of the file resource returned from the fopen call.
foreach($output as $var) <--- in a foreach the first argument MUST be an array!, it is a string right now and so reports an error.
{
echo " ";
echo"display each value as array ";
echo $var;
}...again more function definitions. string file_get_contents ( string $filename [, bool $use_include_path = false [, resource $context [, int $offset = -1 [, int $maxlen = -1 ]]]] ) if he writes his code like this..... $output = file_get_contents('path/to/file.txt'); //<--- Reads entire file into a string, so $output is a string and contains the files content. $output = explode(",",$output); //<-- Split $output by "," and returns an array, $output is an array. // these are moved outside of foreach loop so they do not repeat with each pass. echo " "; echo "display each value as array"; foreach($output as $fragment) { echo $fragment."<br>"; // this will print each "fragment" of the variable array $output. // <br> is for browser output, change to \n for consol output. } ----------------- BTW if he wants to read each and every c
-
the php function defines for reference. --------- fopen function.... resource fopen ( string $filename , string $mode [, bool $use_include_path = false [, resource $context ]] ) --------- explode function.... array explode ( string $delimiter , string $string [, int $limit ] ) --------- foreach looping construct.... foreach (array_expression as $value) statement foreach (array_expression as $key => $value) statement now step through his code....
$output=fopen("testfile.txt","r") or exit("Unable to open file!"); <--- $output is a file resource, this is not the file content.
//(2) Then we can get rid of the"," in there:
$output = str_replace(","," ", $output); <--- he takes the $output which is a file resource and does a string replace on it and re-assigns the variable $output as a string.
//(3) Then we explode it at every ,encountered
//$output = explode(",",$output); <--- when this is uncommented it will change a string into an array. If the source string has no "commas" in it it will return an array with one value. you could also say if(count($output)==1)) {echo "I have 1!";}else{echo "I'm Lost!";}, but currently it does nothing as it is commented out so $output should still be a string.
echo "output"; <--- echos a string called "output"
echo $output; <--- echoes a variable currently a string and most likely just the name of the file resource returned from the fopen call.
foreach($output as $var) <--- in a foreach the first argument MUST be an array!, it is a string right now and so reports an error.
{
echo " ";
echo"display each value as array ";
echo $var;
}...again more function definitions. string file_get_contents ( string $filename [, bool $use_include_path = false [, resource $context [, int $offset = -1 [, int $maxlen = -1 ]]]] ) if he writes his code like this..... $output = file_get_contents('path/to/file.txt'); //<--- Reads entire file into a string, so $output is a string and contains the files content. $output = explode(",",$output); //<-- Split $output by "," and returns an array, $output is an array. // these are moved outside of foreach loop so they do not repeat with each pass. echo " "; echo "display each value as array"; foreach($output as $fragment) { echo $fragment."<br>"; // this will print each "fragment" of the variable array $output. // <br> is for browser output, change to \n for consol output. } ----------------- BTW if he wants to read each and every c
-
I see, it was a resource indeed. I think he also tried to replace the spaces with a comma but did it the other way around. The comma (if there was any) was replaced by spaces then he used explode with comma as a delimiter.
it may indeed be what you suggest with the commas, but at any rate even if he did it that way he would end up with words being split and not the characters. One of the nice things with strings in php is you can treat them as an array and just step through them.
-
Posted Today, 12:07 AM Hi All Thanks in advance for the help, I have the PHP code below to read contents of the file. //The code does not work, it returns the output below " outputResource id #3 Quote Warning: Invalid argument supplied for foreach() in C:\XAMPP\htdocs\kampcoordinators\test.php on line 10"
There might be error on compatibility of your variables. Thanks, Scott solar power lights