1

I need to load some 3rd party widget onto my website. The only way they distribute it is by means of clumsy old <iframe>.

I don't have much choice so what I do is get an iframe html code, using a proxy page on my website like so:

$iframe = file_get_contents('http://example.com/page_with_iframe_html.php');

Then I have to remove some specific parts in iframe like this:

$iframe = preg_replace('~<div class="someclass">[\s\S]*<\/div>~ix', '', $iframe);

In this way I intend to remove the unwanted section. And in the end i simply output the iframe like so:

echo ($iframe);

The iframe gets output alright, however the unwanted section is still there. The regex itself was tested using regex101, but it doesn't work.

6
  • 1
    Use DOMDocument for parsing HTML content instead of regex. Commented Apr 27, 2017 at 16:19
  • Can you share your HTML content? Commented Apr 27, 2017 at 16:20
  • @SahilGulati not really, but here is equivalent <div class="someclass"> <span class="hot-line-text"> hotline: </span> <a id="hot-line-tel" class="hot-line-link" href="tel:0000" target="_parent"> <button class="hot-line-button"></button> <span class="hot-line-number">0000</span> </a> </div> Commented Apr 27, 2017 at 16:26
  • 1
    and what is your expected output? Commented Apr 27, 2017 at 16:27
  • @SahilGulati sorry, the code above is the one that i need to remove. The output code is much larger. All i need to do is to remove the code in my previous comment from the rest of the output Commented Apr 27, 2017 at 16:30

1 Answer 1

1

You should try this way, Hope this will help you out. Here i am using sample HTML remove the div with given class name, First i load the document, query and remove that node from the child.

Try this code snippet here

<?php

ini_set('display_errors', 1);
//sample HTML content
$string1='<html>'
        . '<body>'
            . '<div>This is div 1</div>'
            . '<div class="someclass"> <span class="hot-line-text"> hotline: </span> <a id="hot-line-tel" class="hot-line-link" href="tel:0000" target="_parent"> <button class="hot-line-button"></button> <span class="hot-line-number">0000</span> </a> </div>'
        . '</body>'
    . '</html>';

$object= new DOMDocument();
$object->loadHTML($string1);
$xpathObj= new DOMXPath($object);
$result=$xpathObj->query('//div[@class="someclass"]');
foreach($result as $node)
{
    $node->parentNode->removeChild($node);
}
echo $object->saveHTML();
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for opening my eyes to DOM.so and all its convenience. This is a progressive approach indeed. My question was not solved however, the query returns nothing, i opened new question here stackoverflow.com/questions/43718492/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.