0

HTMLコードをフィルタリングしようとしています

コードには行クラスを持つ div が含まれています

これらの div をコンテンツに置き換えたい

例:

<div class="row anotherClass">some html code 1</div>
<div class="row anotherClass">some html code 2</div>
<div class="row anotherClass">some html code 3</div>

出力は次のようになります

some html code 1
some html code 2
some html code 3

私は次の式を書きました(私は正規表現があまり得意ではありません)が、出力htmlはまだ十分にフィルタリングされていません。

$output = preg_replace_callback('/<div class="row (.*?)">(.*)<\/div>/s', function ($matches) {
            return $matches[2];

        }, $output);
4

1 に答える 1

1

これですか?

<?php

$html = '<div class="row anotherClass">some html code 1</div>
<div class="row anotherClass">some html code 2</div>
<div class="row anotherClass">some html code 3</div>';

$reg = '(<div class="row anotherClass">(.*?)</div>)';
preg_match_all($reg, $html, $divs);

$div_contents = $divs[1];
$divs = $divs[0];

$replaced_html = $html;

for ($i=0; $i < count($divs); $i++) {
    $replaced_html = str_replace($divs[$i], $div_contents[$i], $replaced_html);
}

echo $replaced_html;

?>
于 2013-02-17T07:24:55.850 に答える