2

我想要一个正则表达式模式来评估手动创建的 html 标签中的 if/else,如下所示:

<if condition="$condition">
    Return value if true
<else/>
    Return value if false
</if>

而且,更具体地说:

<if condition="$condition">
    Return value if true
<elseif condition="$another_condition"/>
    Return value if the above is true
<elseif condition="$more_condition"/>
    Return value if the above is true
<else/>
    Return value if non of the above is true
</if>

基本上,我想获取所有“条件”的反向引用返回值,以及“返回值”,以练习将 php 变量打印到 html 文件。例子:

  • 在第一个中,$2 是 $condition,$3 是返回值,如果为真,则为 $4,如果为假
  • 在第二个中,$2 是 $condition,$3 是如果为真,$4 是第二个条件,$5 是上述条件如果匹配的返回值,以此类推。

我需要一个可以重新调整的模式(如果有的话)

<else/>

或者

<else condition="blah">

或多次出现,以正确返回反向引用的值。

这样做的目的是使用一个 php 文件(带有预定义变量)来包含一个 html 模板,并根据 php 变量将值打印出来,而不是直接使用

<?php if ($condition) { $result; } ?>

在 html 模板中。

例子:

PHP 文件:

<?php

  function callback()
  {
    $precondition  = eval('return ' . $matches[1] . ';');

    // Prewritten function for parsing boolean from string
    $condition = parse_boolean($precondition); 


    // Restrict result to boolean only
    if (is_bool($condition))
    {
        // This need better coding based on multiple <elseif ../>
        $ret = ($condition ? $matches[2] : $matches[4]);
    }

    return $ret;      
  }

  $a = 5;
  $b = 6;

  $content = file_get_contents('/html/file/path.html');

  $pattern = 'I need this pattern';
  $output  = preg_replace_callback($pattern, 'callback', $content);

  echo $output;
?>

HTML 文件:

<if condition="$a == $b">
    a is equal with b
<elseif condition="$a > $b">
    a is larger than b
<elseif condition="$a < $b">
    a is smaller than b
</if>

运行 PHP 文件时,它将打印:

a is smaller than b

我希望得到一个具体的答案,或者任何其他方法来修复我自己编写的代码(不是很好恕我直言)。

谢谢你。

4

2 回答 2

1

尝试这样的事情:

PHP

$file = file_get_contents('regexhtml.html');
$html = new DOMDocument();
$html->loadHTMLFile('regexhtml.html');

$conditions = array();
$matches = array();

$ifs = $html->getElementsByTagName('cond');

foreach ($ifs as $if_s) {
     // If
     $ifs1 = $if_s->getElementsByTagName('if');

     $counter = 0;
     foreach ($ifs1 as $if_s1) {
        $conditions[count($conditions)]['if_'.$counter] = array(
            'condition' => $if_s1->getAttribute('condition'),
            'message' => trim($if_s1->nodeValue)
        );
        $counter++;
     }


     // Else-if
     $ifs2 = $if_s->getElementsByTagName('elseif');

     $counter = 0;
     foreach ($ifs2 as $if_s2) {
        $conditions[count($conditions)]['elseif_'.$counter] = array(
            'condition' => $if_s2->getAttribute('condition'),
            'message' => trim($if_s1->nodeValue)
        );
        $counter++;
     }

     // Else
     $ifs3 = $if_s->getElementsByTagName('else');

     $counter = 0;
     foreach ($ifs3 as $if_s3) {
        $conditions[count($conditions)]['else_'.$counter] = array(
            'message' => trim($if_s1->nodeValue)
        );
        $counter++;
     }
}

echo '<pre>';
print_r($conditions);
echo '</pre>';

您会收到一些警告,因此请error_reporting('E_NONE');忽略无效的 HTML 警告。只要您有 xHTML/HTML5 投诉浏览器,它就应该支持自定义标签。

HTML (regexhtml.html)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Title</title>
</head>
<body>
    <cond>
        <if condition="$a == $b">
            a is equal with b
        </if>
        <elseif condition="$a > $b">
            a is larger than b
        </elseif>
        <elseif condition="$a < $b">
            a is smaller than b
        </elseif>
        <else>
            a is smaller than b
        </else>
    </cond>
</body>
</html>

输出

Array
(
    [0] => Array
        (
            [if_0] => Array
                (
                    [condition] => $a == $b
                    [message] => a is equal with b
                )

        )

    [1] => Array
        (
            [elseif_0] => Array
                (
                    [condition] => $a > $b
                    [message] => a is equal with b
                )

        )

    [2] => Array
        (
            [elseif_1] => Array
                (
                    [condition] => $a < $b
                    [message] => a is equal with b
                )

        )

    [3] => Array
        (
            [else_0] => Array
                (
                    [message] => a is equal with b
                )

        )

)

您现在可以随心所欲地处理输出。剥离它以制作正则表达式或任何你想用它做的事情。

要匹配 PHP 变量(如$a),您可能需要一些范围操作;

// These won't practically exist, but only as strings in the Array() object
$html_a1 = '12';
$html_b1 = '23';

// Storing the string-based code into new variables
$a1 = eval('return $html_a1;');
$b1 = eval('return $html_b1;');

echo eval('return $html_a1;').'<br /><br />';

if ($a1 == $b1) {
    echo 'a is equal with b';
}
elseif ($a1 > $b1) {
    echo 'a is larger than b';
}
elseif ($a1 < $b1) {
    echo 'a is smaller than b';
}
else {
    echo 'Something else';
}

使用该DOMDocument()库,您也可以根据需要立即写回结果(http://www.php.net/manual/en/class.domdocument.php),因此您无需手动重建 HTML。该库还允许您写回被操纵的 HTML。

祝你好运!

于 2013-03-24T18:32:50.453 回答
0

也许这会对你有所帮助,我使用preg_replace()有多种模式,我假设你信任创建“HTML 文件”的人:

$a = 5;
$b = 7;
$html = '<if condition="$a == $b">
    a is equal with b
<elseif condition="$a > $b">
    a is larger than b
<elseif condition="$a < $b">
    a is smaller than b
</if>';

$new_html = preg_replace(array('/<if condition="(.*)">\s*(.*)/', '/<elseif condition="(.*)">\s*(.*)/', '/<\/if>/'), array('if($1){'.PHP_EOL.'echo "$2";', '}elseif($1){'.PHP_EOL.'echo "$2";', '}'), $html);
eval($new_html); // result
var_dump($new_html); // This is to view the "final" code
于 2013-03-24T18:33:58.277 回答