Skip to content

Instantly share code, notes, and snippets.

@juniorb2ss
Created August 13, 2014 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juniorb2ss/eda182e5394df521604d to your computer and use it in GitHub Desktop.
Save juniorb2ss/eda182e5394df521604d to your computer and use it in GitHub Desktop.
<?php
class Template {
private $words;
public function SetKeywords($Setkeyword, $Setvalue){
if(gettype($this->words) != 'array'){
$this->words = array();
}
$array = array($Setkeyword => $Setvalue);
$this->words = array_merge($array,$this->words);
}
public function ReplaceKeywords($maskContent) {
if($this->words){
if(!$maskContent || strlen($maskContent) == 0)
return $maskContent;
foreach($this->words as $keyword=>$value)
{
$keyword = '{$'.$keyword.'}';
$maskContent = str_replace($keyword, $value, $maskContent);
}
return $maskContent;
}
else
{
return false;
}
}
public function GetReplaceKeywordCount($maskContent){
if(!$maskContent || strlen($maskContent) == 0)
return $maskContent;
$count = array();
foreach($this->words as $keyword=>$value){
$searchWord = '{$'.$keyword.'}';
$wordCount = substr_count($maskContent, $searchWord);
if($wordCount > 0)
$count[$keyword] = $wordCount;
}
return $count;
}
}
$template = new Template;
$file_include = 'menu aqui'; // pegando o arquivo menu file_content etc...
$template->SetKeywords('hello_world', 'hello_world');
$template->SetKeywords('menu', $file_include);
$file = '{$hello_world} template // {$menu}';
var_dump($template->GetReplaceKeywordCount($file));
$file = $template->ReplaceKeywords($file);
echo $file; // return file
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment