バルクテンプレート(1つのファイルに複数のテンプレートエントリ)をロードし、それに応じて保存できるテンプレート風のシステムがあります。問題は、現在のアプローチがを使用していることでpreg_replace()
ありeval
、それは本当にエラーが発生しやすいです。このエラーの例は、正規表現を壊して解析エラーを作成する不適切に配置された文字である可能性があります。
Parse error: syntax error, unexpected '<' in tsys.php: eval()'d code
これを行うコードは次のとおりです。
// Escaping
$this->_buffer = str_replace( array('\\', '\'', "\n"), array('\\\\', '\\\'', ''), $this->_buffer);
// Regular-expression chunk up the input string to evaluative code
$this->_buffer = preg_replace('#<!--- BEGIN (.*?) -->(.*?)<!--- END (.*?) -->#', "\n" . '$this->_tstack[\'\\1\'] = \'\\2\';', $this->_buffer);
// Run the previously created PHP code
eval($this->_buffer);
このバルクテンプレートのサンプルファイルは次のようになります。
<!--- BEGIN foo -->
<p>Some HTML code</p>
<!--- END foo -->
<!--- BEGIN bar -->
<h1>Some other HTML code</h1>
<!--- END bar -->
この入力でコードを実行すると、次の$this->_tstack
2つの要素が与えられます。
array (
'foo' => "<p>Some HTML code</p>",
'bar' => "<h1>Some other HTML code</h1>",
);
これは予想される動作ですが、の必要性をなくすことができる方法を探していeval
ます。