Jump to content

[SOLVED] problem with combination of substr/strpos when searching for string.


Bramme

Recommended Posts

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...

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.