Page 1 of 1

Parsing the file before including it

Posted: Wed Oct 24, 2007 4:57 pm
by nutcracker
Hello there!

I would like to make a module, which would parse the whole php file for my own code and possibly insert the text in the different places in it. It would check for the other included modules, which would do the actual parsing, each module for it's own purpose. Now I'm a bit indecisive how to start the whole thing. It somehow seems to me the approach with the reading the file, parsing it and then printing it out is very time consuming (if I'd do it for each article I have). These articles probably won't have the php code, so I probably won't need to use the eval() function (just the plain "echo" output). What do you guys think about this? Is this approach (reading / parsing / writing) totally stupid and time consuming or should I give it a go? Are there already some better approaches (I'm pretty new to php)?

Thanks.

Posted: Wed Oct 24, 2007 5:16 pm
by feyd
Modifying a script on-the-fly? Probably not a good idea. This needs some rethinking.

For example, one could pass data to the class/functions/pie-bits with details that would allow them to choose some generalized code for execution.

It sounds more like you're talking about templating. Look into Template Lite if you are.

Posted: Thu Oct 25, 2007 10:08 am
by nutcracker
Thank you for your answer.

I have checked out Template Lite and Smarty, before that I checked some tutes about templating to even have a bit of clue what I'm looking at. I didn't have much time for checking it (as I work the whole day), but even this small amount of time looking into it brought me doubts if this is the stuff I'm looking for. I will most definitely have a detailed look into it when I find a bit of time. Thanks for that info.

Let me be more specific on my goals. I'd like to accomplish two things:

The first would be to make a module, which would detect all the headers (<hX>...</hX>) in my article, and then generate the code for the table of contents at the beginning of the file. It's links would point to the <a name=...> tags automatically positioned above every header.

The second is the module, which would check my code for some special tags (let's say [ref="1"]) and according to the tag would generate a reference section at the end of the page from the list of references situated in a mysql table. So the reader would see the reference numbers in the text placed in the square brackets, and in the end of the document would be a list of all the used references (not all the references from that table).

Note that those articles wouldn't contain ANY php code, just the html tags for the layout, connected with the external css files and the contents of an article. So I wouldn't actually be modifying any PHP script, just HTML.

As I still don't know about the powers of templating, I'm not sure if I could do this with it. If you have any idea how to do it, please give me some hints how to start the right way. I'll do the rest :) . Thanks.

Posted: Thu Oct 25, 2007 10:28 am
by feyd
This sounds more like a Wikiwiki parser.

viewtopic.php?p=399010#399010

Posted: Thu Oct 25, 2007 10:37 am
by John Cartwright
A simple regular expression of the data on the fly to build the table of contents is as simple as

Code: Select all

preg_match_all('#<hX>(.*?)</hX>#im', $input, $matches);

echo '<pre>';
print_r($matches[1]);

Posted: Thu Oct 25, 2007 12:48 pm
by nutcracker
Thank you guys for your time and effort. So I'll just go with the regular expressions and try to code it so it works as fast as I'm able to code it. Then we'll see the outcome on some tests.