Bramme Posted November 30, 2007 Share Posted November 30, 2007 Okay, I'm building my own little CMS for later use, meanwhile experimenting a little with classes etc. I have a folder which contains "module" files, each file will have specific functionalities, like a file content.php to edit the content of the site, a blog.php to edit a blog etc. Now I'm writing a function that reads those files from their directory and checks the file for certain strings: a php comment that indicates it's an actual module file and then 3 xml-like tags that define the name of the module, an abbreviation to use in the url's and what items it should list in a subnav. For example, content.php looks like this: <?php /* Modular Module */ /** Properties <cat>content</cat> <name>Content</name> <subnav>'Add, Edit, Delete page'</subnav> */ ..... my php functions work like this: $k = 0; while (false !== ($file = readdir($handle))) { $modContent = file_get_contents($this->modulesDir.$file); if($this->EvalFile($file)) { if($this->isModule($modContent)) { $this->modules[$k]['filename'] = $file; $this->modules[$k]['name'] = $this->GetModuleProperty($modContent, 'name'); $this->modules[$k]['navcat'] = $this->GetModuleProperty($modContent, 'cat'); $this->modules[$k]['subnav'] = $this->GetModuleProperty($modContent, 'subnav'); } } $k++; } The getmodule property is the one that goes looking for what's between the tags: private function GetModuleProperty ($content, $tag) { $startTag = '<'.$tag.'>'; $endTag = '</'.$tag.'>'; $start = strpos($content, $startTag) + strlen($startTag); $length = strpos($content, $endTag) - $start; $property = substr($content, $start, $length); return $property; } Full class file if needed Now this is working perfectly for the name and subnav tags, but for some odd reason it won't work with the cat tags. I've renamed, changed what's in them... If I print_r($modules) i get this: See online example. Also, could anyone tell me why the indexes (defined by $k) of my array are 1 and 3 instead of 0 and 1. There's only 2 files in the directory, but it's as if the code checks each file once (adding 1 to $k) and when it checks a second time it adds it to the array... Quote Link to comment Share on other sites More sharing options...
Barand Posted November 30, 2007 Share Posted November 30, 2007 Have a look at www.php.net/glob as an alternative to readdir(), it may help with your $k problem if you specify "*.php". Alternatively, move the $k++ so it only increments when both if() conditions are satisfied. Quote Link to comment Share on other sites More sharing options...
Bramme Posted November 30, 2007 Author Share Posted November 30, 2007 Have a look at www.php.net/glob as an alternative to readdir(), it may help with your $k problem if you specify "*.php". Alternatively, move the $k++ so it only increments when both if() conditions are satisfied. thanks, moving $k++; fixed the key issue, but I'm still lost about what's happening with my <cat> tags... I don't know if glob could help me, i'm guessing the files you want to search have to be in the same folder as the executing script? Quote Link to comment Share on other sites More sharing options...
Barand Posted November 30, 2007 Share Posted November 30, 2007 I don't know if glob could help me, i'm guessing the files you want to search have to be in the same folder as the executing script? You can specify a "path/*.php" Quote Link to comment Share on other sites More sharing options...
Bramme Posted November 30, 2007 Author Share Posted November 30, 2007 Okay, this is why I really should start using another php editor than Dreamweaver. I was editting the wrong module files. I feel stupid now. Quote Link to comment Share on other sites More sharing options...
Barand Posted November 30, 2007 Share Posted November 30, 2007 Eclipse is good (and free). I'll send you a download link when I get to the office on Monday if you want. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.