@blog.justoneplanet.info

日々勉強

PHPで不正な閉じタグを見つける

ループは1回にできると思うけどまぁとりあえず。

<?php
$str = <<<EOD
<html>
<body>
<center>
<form action="confirm.php" method="get">
<input type="text" name="name"><br>
<input type="submit" value="submit">
</center>
</form>
</body>
</html>
EOD;

preg_match_all("/<.*?>/", $str, $matches);
//var_dump($matches);
$ary = array();
$stack = array();

foreach ($matches[0] as $key => $value) {
    array_push($ary, $value);
}

foreach ($ary as $key => $value) {
    if (preg_match("/<\/([a-zA-Z]+)/", $value, $tag)) {
#        echo "$value\n";
#        var_dump($tag[1]);
        $isFoundClose = false;
        foreach ($ary as $k => $v) {
            if (preg_match("/<{$tag[1]}/", $v)) {
#                echo "match\n";
                $isFoundClose = true;
                array_splice($ary, $k, $key - $k);
                break;
            }   
        }   
        if (!$isFoundClose) {
            echo "error:$value\n";
        }   
    }   
    else {

    }   
}

コメントはまだありません»

No comments yet.

RSS feed for comments on this post.TrackBack URL

Leave a comment