codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
<?php class string { 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; } } $string = new string; $string->SetKeywords('DB_TYPE', 'MySQL'); $string->SetKeywords('DB_USER', 'root'); $arrayConfig = "$configs = [ 'DB_TYPE' => '{DB_TYPE}', 'DB_USER' => '{DB_USER}' ]"; $string = $string->ReplaceKeywords($arrayConfig); echo $string; // return file
Private
[
?
]
Run code
Submit