Question

In my text the user can input modules which look like this; [[form:contact]], representing a module and its name; [[module:name]

I use the following code to retract this from the text, and split it, get the information from the database I want and replace it.

Is this the best way? It works good, but I'm not sure if this is the most efficient way, and was wondering whay you guys would think of it...

I'm working with the Zend Framework.

This is the code:

$start = strpos($this->view->page->text, "[[");
$end = strpos($this->view->page->text, "]]");
$length = $end-$start+2;

$getModuleTag = substr($this->view->page->text, $start, $length);
$length = $end-$start-2;
$removeTags = substr($getModuleTag, 2, $length);

$split = strpos($removeTags, ":");

$GetModuleSort = substr($removeTags, 0, $split);
$GetModuleName = substr($removeTags, $split+1);

// Get the wanted data from the database
switch($GetModuleSort)
{
    case 'form':
        $this->result = $this->formsService->GetFormByName($GetModuleName);
        $replaceTag = $this->result->elements;
    break;
}

// TEST OUTPUT
$final = str_replace($getModuleTag, $replaceTag, $this->view->page->text);
$this->view->finalOutput = $final;
Was it helpful?

Solution

Yes, it's a good way to do it.

OTHER TIPS

You could use a regular expression- depending on your needs:

$pattern="/\[\[(.*?)\]\]/";
$string="some string with a [[module]] in it, and [[another]] here";
$match=preg_match_all($pattern, $string, $matches);
print_r($matches);

Would give an array of all the modules/identifiers:

Array ( [0] => Array ( [0] => [[module]] [1] => [[another]] ) [1] => Array ( [0] => module [1] => another ) ) 

The first child includes the extracts as array children with the brackets (if you need these) the second child contains an array of the contents....

This would work with as many references as you wish.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top