How to search several PHP files for certain values??

Discussion in 'PHP' started by LongHaul, Oct 5, 2006.

  1. #1
    I have a directory that has a bunch of .php files, each of which containes several variables (all the same variable names, with different values plugged in).

    I want to have a php file that searches through all the files in the directory, and prints the value of $longhaul from each file.

    I have the loop to search the files working, it's just calling up a variable from each file that I can't figure out.

    It looks something like this now (variables in green):

    <?php
    		if ([COLOR="Green"][B]$handle[/B][/COLOR] = opendir('path/to/my/directory')) {
         	while (false !== ([B][COLOR="green"]$file[/COLOR][/B] = readdir([COLOR="green"][B]$handle[/B][/COLOR]))) {
           [B][COLOR="Red"]???[/COLOR][/B]
           print ([COLOR="green"][B]$longhaul[/B][/COLOR]);
           }
         }
       ?>
    Code (markup):
    It's actually more complicated than that, but I have the other stuff working. Any ideas?

    Thanks! :)
     
    LongHaul, Oct 5, 2006 IP
  2. coolsaint

    coolsaint Banned

    Messages:
    257
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Dear LongHaul,

    I wish you want to search character by character in the php files to match what you need to match or you want to search the character in the filename. it is not clear to me . Don't you think you have to open the file in read mode to search through it? Can u specify some more code snippet and describe what u really need. It's not clear to me.

    Thanks.

    Coolsaint
     
    coolsaint, Oct 5, 2006 IP
  3. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It could pay to use the PHP tokenizer to interpret the PHP file for you, then search through all of what it returns to determine where the assignment is then just read the value out after it.

    That might be a bit of overkill, but definitely usable.

    It all really depends on how consistent your assignment code is over all of the files. If it's fairly similar, you could use a regular expression to find the assignment then just rip that apart...
     
    TwistMyArm, Oct 5, 2006 IP
  4. daboss

    daboss Guest

    Messages:
    2,249
    Likes Received:
    151
    Best Answers:
    0
    Trophy Points:
    0
    #4
    not 100% sure what you want... but from what i can decipher, you can download everything onto your laptop and then use good old windows explorer's search function to search text within the files... ;)

    hope that answered your question
     
    daboss, Oct 5, 2006 IP
  5. LongHaul

    LongHaul Peon

    Messages:
    670
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #5
    OK, let me try to make it clearer!

    I will have a website that has a section of book reviews.

    The book review files are all very short php files, no html. Like this:

    <?php
    $title = "";
    [COLOR="Red"]$author[/COLOR] = "";
    $publisher = "";
    $review = "";
    include ("[B]review.php[/B]");
    ?>
    Code (markup):
    I fill in the variables for each book, each with its own file. For example:

    christine.php:
    <?php
    $title = ""Christine";
    [COLOR="red"]$author[/COLOR] = "Stephen King";
    $publisher = "Harper Row";
    $review = "Good book, but too long.";
    include ("[B]review.php[/B]");
    ?>
    Code (markup):
    ["review.php" is the file with all the html.]

    So, I want to have a page on my site only for Stephen King - stephenking.php. As I add reviews, I want this page to always be current - on loading, it should check all the .php files in my "/reviews/" folder and look at the $author of each one. If a file's $author == "Stephen King", it will echo the other info from that file and make a list in a format I choose, for example like this:

    Then I'll have another page for JK Rowling, another for Michael Crichton, etc.

    So I need some php coding in my stephenking.php file to check the files, test the $author variable from each one, then read the rest of the variables from that file if there is a match.
     
    LongHaul, Oct 5, 2006 IP
  6. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Look at the file() function: it will read the file in to an array. Then, either loop through the array looking for a value that contains '$author =' or jump straight to the value if it always on the same line.

    From there, there's a number of things you can do. explode() on the equal sign, string replace the '$author = "' bit or whatever. There's a few different ways to do it.

    What I'm more worried about, though, is why you're not using a database instead?
     
    TwistMyArm, Oct 5, 2006 IP
  7. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #7
    Very interesting approach. I think you need a database. Sure you can write a function with file_get_contents for each file, then look for author in it with regex and so get your results. But it will be slow, so you need consider caching. You can update your cache as your information changed or by cron.

    By I think it would be better to switch to database. Databases designed for storing and seeking data like this.
     
    wmtips, Oct 5, 2006 IP
  8. LongHaul

    LongHaul Peon

    Messages:
    670
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thanks, I'll play around with file().


    Yeah, I know. I have been trying to avoid doing that since I know NOTHING about databases, and I keep pushing back the publishing of my site because of things like deciding to do php! That would push it back further. I guess if it's the best thing I should look into it, but I don't yet even know where to start.

    I started putting php on my site for a site-wide menu, then it just kept growing ;)

    Maybe I should look into a database. Any idea where or what? :)
     
    LongHaul, Oct 5, 2006 IP
  9. penagate

    penagate Guest

    Messages:
    277
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Most web hosts offer MySQL which is a free and fairly simple solution.
    You can also install it locally if that is where you are doing you development.

    Databases are really very simple, they only get complex if you have a large performance intensive application or need to extract data from complicated storage schemas.

    There are a number of ways to connect to a database from PHP, if you are running PHP 5 you may be able to use PDO which is the preferred option, or PEAR::MDB2 on PHP 4. Failing that look for mysqli and failing even that you can use the bog standard mysql_* functions which are the staple of all craptastic web tutorials.

    The PDO/mysqli pages on php.net have all the references you need to learn how to use the APIs.

    Also you will find a tool such as phpMyAdmin handy to administer your databases from a GUI perspective. Your host will doubtless provide some solution in that regard.

    Let us know if you have any further queries.

    - P
     
    penagate, Oct 5, 2006 IP
  10. LongHaul

    LongHaul Peon

    Messages:
    670
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Thanks! My host, hostgator, does indeed offer MySQL. And when I installed a php thing on my local computer I think it may have installed MySQL too. Maybe, I'll have to check.

    Anyway you guys have convinced me, I'll step into the database world. I wonder if the concept has changed much since the Atari 1200XL ;)
     
    LongHaul, Oct 5, 2006 IP
  11. penagate

    penagate Guest

    Messages:
    277
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #11
    If you installed WAMP or XAMPP then yes it includes MySQL.
     
    penagate, Oct 5, 2006 IP
  12. LongHaul

    LongHaul Peon

    Messages:
    670
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #12
    It was MAMP, on a Mac. I'm at work now so I don't know, but I assume it's there.
     
    LongHaul, Oct 6, 2006 IP
  13. LongHaul

    LongHaul Peon

    Messages:
    670
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #13
    OK I'm trying to set up a MySQL table and I just wanted to take time out to do some bitching, to let it out of my system:

    The official MySQL online documentation sucks. php.net's is great, but this sql shit drones on and on about some kind of terminal command-line commands. But I'm doing this in a web-based environment from my cpanel, and i can't find out SIMPLE INFORMATION like what the hell null is and why I am getting so many errors trying to create tables and fields.

    It doesn't seem geared to beginners. Actually I can't tell who would benefit from this. High-end programmer types, maybe. Not me.

    OK that's it. I'll go over to the sql forum form now on. Thanks :|
     
    LongHaul, Oct 6, 2006 IP