简体   繁体   中英

Delete all divs except a div with a specific class?

If i have this setup on website 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:

<?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

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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