简体   繁体   English

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

[英]Delete all divs except a div with a specific class?

If i have this setup on website 1: 如果我在网站1上有此设置:

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

If i then use this on website 2: 如果我然后在网站2上使用它:

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

I want it to post: 我要它发布:

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

but all i get is 但我得到的只是

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

How do I do this better? 我该如何做得更好?

I suggest you to use dom parsing here something like 我建议您在这里使用dom解析

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

But Still the solution to your problem is to replace 但是,解决您的问题的方法仍然是更换

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

with

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

because you are using class in your pattern which is there only in first div 因为您在模式中使用的类仅在第一格中存在

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM