If I use the file() function to read a html file. Then it makes an array of type string. But how do I know how big it is. Index from 0 to ????
Thanks in advance.
Printable View
If I use the file() function to read a html file. Then it makes an array of type string. But how do I know how big it is. Index from 0 to ????
Thanks in advance.
Use the count() function:
But if you're just going to echo it, then do this:Code:$file = file('file.txt');
for ($i = 0; $i < count($file); $i++) {
echo $file[$i] . "<br>\n";
}
That way you don't even have to mess with the array.Code:$file = implode("\n", file('file.txt'));
echo $file;
Note: I can't remember if you need to implode it with a newline ("\n") or empty string (""). I believe a newline will cause it to be double spaced, so if that's a case, just use an empty string.
Thanks I will try that in a moment. I am going to search for a word in that file. So I guess that the first "function" will work for me.
Thanks....
I'm not exactly sure what you're doing, but if you're just searching for a word, I think the second way would be better. I'd try to avoid loops when possible:
Code:$file = implode("\n", file('file.txt'));
if (strpos($file, "dude") === false) {
echo "String not found!";
} else {
echo "String found at " . strpos($file, "dude") . "!";
}
I know this may be a bit more then I should try too pull of in PHP. Becuase I am pretty new to PHP. But I am trying to make an easy search engine for our page. That will open up and read all the html files and look for a word in the files. And if it finds it, it will print a link to that/those page(s)....
I would still recommend my second method.Quote:
Originally posted by NoteMe
I know this may be a bit more then I should try too pull of in PHP. Becuase I am pretty new to PHP. But I am trying to make an easy search engine for our page. That will open up and read all the html files and look for a word in the files. And if it finds it, it will print a link to that/those page(s)....
Yes it looks good this way..
PHP Code:$file = implode("\n", file('file.txt'));
if (strpos($file, "dude") === false) {
echo "String not found!";
} else {
echo "String found at " . strpos($file, "dude") . "!";
}
BTW I didn't know that you could have thre === after each other. What is the diffrence with three???
I would recomend the manual if you have any questions.
$a === $b
Identical TRUE if $a is equal to $b, and they are of the same type. (PHP 4 only)
I am using Wrox Beginning PHP. But it isn't good to look up in. It is doing to much thing for you, and you have to read a whole php page example if oyu want to learn about a small function. So I will try the manual. I guess you are reffering to the one at PHP.net