Results 1 to 9 of 9

Thread: Array question....

  1. #1

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    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.

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    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.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    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....

  4. #4
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    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") . "!";
    }
    My evil laugh has a squeak in it.

    kristopherwilson.com

  5. #5

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    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)....

  6. #6
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    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.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  7. #7

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    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???

  8. #8
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    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)

  9. #9

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    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
  •  



Click Here to Expand Forum to Full Width