|
-
Feb 16th, 2003, 12:33 PM
#1
Array question....
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.
-
Feb 16th, 2003, 01:17 PM
#2
Stuck in the 80s
Use the count() function:
Code:
$file = file('file.txt');
for ($i = 0; $i < count($file); $i++) {
echo $file[$i] . "<br>\n";
}
But if you're just going to echo it, then do this:
Code:
$file = implode("\n", file('file.txt'));
echo $file;
That way you don't even have to mess with the array.
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.
-
Feb 16th, 2003, 01:22 PM
#3
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....
-
Feb 16th, 2003, 01:26 PM
#4
Stuck in the 80s
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") . "!";
}
-
Feb 16th, 2003, 01:30 PM
#5
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)....
-
Feb 16th, 2003, 01:50 PM
#6
Stuck in the 80s
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)....
I would still recommend my second method.
-
Feb 16th, 2003, 01:53 PM
#7
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???
-
Feb 16th, 2003, 02:16 PM
#8
Frenzied Member
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)
-
Feb 16th, 2003, 02:21 PM
#9
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|