删除除具有特定类的div之外的所有div?

时间:2013-03-26 04:11:08

标签: php replace preg-match-all

如果我在网站1上进行了此设置:

<div class="awesomeContent">
    <div>
        Information goes here...
    </div>
    <div>
        Information goes here...
    </div>
    <div>
        Information goes here...
    </div>
</div>

如果我在网站2上使用它:

<?php
$data = file_get_contents('http://website.com/hello);
preg_match_all ("/<div class=\"awesomeContent\">([^`]*?)<\/div>/", $data, $matches);
print_r($matches[0]);

我希望它发布:

<div class="awesomeContent">
    <div>
        Information goes here...
    </div>
    <div>
        Information goes here...
    </div>
    <div>
        Information goes here...
    </div>
</div>

但我得到的只是

<div class="awesomeContent">
    <div>
        Information goes here...
    </div>

我该如何做得更好?

1 个答案:

答案 0 :(得分:0)

我建议你在这里使用dom解析

$parsedHtml = simplexml_load_string($data);
print_r($parsedHtml)`

但问题的解决方案仍然是 取代

 "/<div class=\"awesomeContent\">([^`]*?)<\/div>/"

  "/<div class=\"awesomeContent\">([^`]*)<\/div>/"

因为您在模式中使用的类只存在于第一个div中