不足しているparse_string()を追加します

画像 3月、 Wave habrayuzerからニュースが登場し、CodeIgniterフレームワークのすべてのファンの心を揺さぶりました。このlyalykaのバージョン2.0が登場します。 そして今、2か月以上が経過しており、まだそこにあり、「dvushka」はオフサイトからダウンロードすることはできません。

そして、すべては大丈夫ですが、フレームワークの2番目のバージョン、Parserクラスのparse_string()で実装されると約束されたメソッドは、ファイルからだけでなく変数からもテンプレートを解析できるようにします。 そして、私の開発の1つでは、小さなテンプレートをデータベースに保存する必要があるため、いわば、私にとっては耐え難いものになりました。

開発者を称賛し、使用するすべての標準クラスを拡張する機会を与えられました。

このメソッドの実装について-Habrokatへようこそ。

そのため、Parserクラスの機能を拡張するには、ディレクトリ「 application / libraries 」にファイル「 MY_Parser.php 」を作成します。 ファイル内に次のコードを挿入します。

<?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
  1. <?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
  2. <?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
  3. <?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
  4. <?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
  5. <?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
  6. <?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
  7. <?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
  8. <?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
  9. <?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
  10. <?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
  11. <?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
  12. <?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
  13. <?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
  14. <?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
  15. <?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
  16. <?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
  17. <?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
  18. <?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
  19. <?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
  20. <?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
  21. <?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
  22. <?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
  23. <?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
  24. <?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
  25. <?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
<?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .


以上です! これで、新しいparse_string()メソッドは、Parserライブラリを接続する場所であればどこからでも使用できるようになります。

このメソッドは、解析メソッドによって要求されたものとほぼ同じパラメーターを受け入れますが、テンプレートファイルへのパスの代わりに最初のパラメーターがテンプレートテキストを含む文字列変数である点が異なります。 2番目のパラメーターも変数を含む配列であり、3番目はメソッドが処理済みのテンプレートを返す方法を示します-変数に返すか、直接出力します。

CodeIgniter 1.7.2へのこの小さな追加が、完全な2.0リリースを待っている間に役立つことを願っています。

また会うまで;)

Source: https://habr.com/ru/post/J95167/


All Articles