PHPを使用して、変数名に応じてテキスト/htmlのセクションを置き換えるために、htmlテキストでタグスタイルの注釈を使用したいと思います。ネストされたタグを使用しない場合、置換自体は完全に機能します。
ただし、ネストされたタグがある場合は、外側のタグのみが置き換えられます。
私の正規表現はこれです:
\[\@if(not)?:([a-zA-Z0-9]+)(?:=(.*?))?\].*?\[\@endif:\2\]
解析するコンテンツの例を使用して、この正規表現の動作を確認できます:
https://regex101.com/r/rE3fL1/2
について読ん(?R)
だことがありますが、それを機能させることができません。真ん中を に
置き換えてみましたが、何も変わりません。.*?
(.*?|(?R))
ネストされたタグもキャプチャするように正規表現を変更するにはどうすればよいですか?
コード: ($this->output
テキストにアクセスします)
public function output($dbAccess = true) {
// only translate when dbaccess is granted
if ($dbAccess)
$this->localize();
// insert values into template
foreach ( $this->values as $key => $value ) {
$tagToReplace = "[@$key]";
$this->output = str_replace ( $tagToReplace, $value, $this->output );
}
// gather conditional content sections from output
$condis = array();
$conmatches = array ();
preg_match_all ( '/\[\@if(not)?:([a-zA-Z0-9]+)(?:=(.*?))?\].*?\[\@endif:\2\]/s', $this->output, $conmatches );
if (count($conmatches) > 0) {
$c = $conmatches[0];
// if (count($c) > 0)
// echo "found " . count($c[0]) . " conditional tpl statement matches!";
for ($i=0; $i<count($c); $i++) {
$text = $c[$i];
$not = $conmatches[1][$i];
$name = $conmatches[2][$i];
$value = $conmatches[3][$i];
$condis[] = new ConditionalContent($text, $not, $name, $value);
}
// substitute conditional content sections
foreach ($condis as $cc) {
// convenience and readability vars!
$varname = $cc->name();
$vals = &$this->values;
$value = $cc->value();
// if condition is bound to value of variable and not just existence
if ($value != "") {
// del if name == exists(value)
if ($cc->not() && isset($vals[$varname])) {
if ($vals[$varname] == $value) {
$this->delContent($cc->content());
}
}
// del if not exists(value) or value != name
else {
if (!isset($vals[$varname]) || $vals[$varname] != $value) {
$this->delContent($cc->content());
}
}
}
else {
if ( isset($vals[$varname]) && $cc->not() ||
!isset($vals[$varname]) && !$cc->not()) {
$this->delContent($cc->content());
}
}
}
// delete all left over if(not) and endif statements
$this->output = preg_replace('/\[@(?:if(?:not){0,1}|endif):[a-zA-Z0-9]+(=.*?)?\]/', '', $this->output);
}
//else { echo "found no conditional tpl statements"; }
return $this->output;
}