PHP How to give block of code a parent node





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I have this html structure;
and I need to remove everything after



<span class="Title_Blue">


or, at least, put the block inside a div. (Then I can manipulate it.)



Is there anyway I can do it with xpath?



I can easily put a tag inside a div, but the problem here is that the code I want to embrace into a div is all at the same level.



Thanks!!



<body>
<div class="article_text">
<div id="text">

<p class="paragraph">
<strong> [text] </strong>
</p>
<div>
<strong> [text] </strong>
</div>
<strong>
<br> [Title] <br>
</strong>
<div style="text-align: justify;">
<br> [text]
</div>
<div style="text-align: justify;">
<br> [text]
</div>

<span class="Title_Blue"> [text] </span>
<br>
<p> ... </p>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>

<span class="Title_Blue"> [text] </span>
<br>
<p> ... </p>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>

<span class="Title_Blue"> [text] </span>
<br>
<p> ... </p>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>

</div>
</div>
</body>


EDITED



I tried this:



$html = $data
$doc = new DOMDocument();
@$doc->loadHTML('<?xml encoding="UTF-8">' . $html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXpath($doc);
$contents = $xpath->query('//*[@class="Title_Blue"]');
foreach ($contents as $content) {
$div = $doc->createElement('div');
$div->setAttribute('class', 'DELETE_ME');
$content->parentNode->replaceChild($div, $content);
$div->appendChild($content);
}


EDITED 2



Was able to solve using this code:



$html = $data
$doc = new DOMDocument();
@$doc->loadHTML('<?xml encoding="UTF-8">' . $html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXpath($doc);
$contents = $xpath->query('//*[@class="Title_Blue"] | //*[@class="Title_Blue"]/following-sibling::*');
foreach ($contents as $content) {
$content->parentNode->removeChild($content);
}


Thanks to Andersson!










share|improve this question

























  • did you try anyting?

    – Madhur Sharma
    Nov 23 '18 at 17:03






  • 1





    You cannot use XPath to remove everything after, but you can use it to select everything after: //span[@class="Title_Blue"]/following-sibling::*

    – Andersson
    Nov 23 '18 at 17:11











  • Yes, it works! Thank you! Anyway I can include the original tag? Because it deletes everything "after". //*[@class="Title_Blue"]/following-sibling::* Maybe "and"

    – Migu3litto
    Nov 23 '18 at 17:18




















1















I have this html structure;
and I need to remove everything after



<span class="Title_Blue">


or, at least, put the block inside a div. (Then I can manipulate it.)



Is there anyway I can do it with xpath?



I can easily put a tag inside a div, but the problem here is that the code I want to embrace into a div is all at the same level.



Thanks!!



<body>
<div class="article_text">
<div id="text">

<p class="paragraph">
<strong> [text] </strong>
</p>
<div>
<strong> [text] </strong>
</div>
<strong>
<br> [Title] <br>
</strong>
<div style="text-align: justify;">
<br> [text]
</div>
<div style="text-align: justify;">
<br> [text]
</div>

<span class="Title_Blue"> [text] </span>
<br>
<p> ... </p>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>

<span class="Title_Blue"> [text] </span>
<br>
<p> ... </p>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>

<span class="Title_Blue"> [text] </span>
<br>
<p> ... </p>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>

</div>
</div>
</body>


EDITED



I tried this:



$html = $data
$doc = new DOMDocument();
@$doc->loadHTML('<?xml encoding="UTF-8">' . $html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXpath($doc);
$contents = $xpath->query('//*[@class="Title_Blue"]');
foreach ($contents as $content) {
$div = $doc->createElement('div');
$div->setAttribute('class', 'DELETE_ME');
$content->parentNode->replaceChild($div, $content);
$div->appendChild($content);
}


EDITED 2



Was able to solve using this code:



$html = $data
$doc = new DOMDocument();
@$doc->loadHTML('<?xml encoding="UTF-8">' . $html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXpath($doc);
$contents = $xpath->query('//*[@class="Title_Blue"] | //*[@class="Title_Blue"]/following-sibling::*');
foreach ($contents as $content) {
$content->parentNode->removeChild($content);
}


Thanks to Andersson!










share|improve this question

























  • did you try anyting?

    – Madhur Sharma
    Nov 23 '18 at 17:03






  • 1





    You cannot use XPath to remove everything after, but you can use it to select everything after: //span[@class="Title_Blue"]/following-sibling::*

    – Andersson
    Nov 23 '18 at 17:11











  • Yes, it works! Thank you! Anyway I can include the original tag? Because it deletes everything "after". //*[@class="Title_Blue"]/following-sibling::* Maybe "and"

    – Migu3litto
    Nov 23 '18 at 17:18
















1












1








1








I have this html structure;
and I need to remove everything after



<span class="Title_Blue">


or, at least, put the block inside a div. (Then I can manipulate it.)



Is there anyway I can do it with xpath?



I can easily put a tag inside a div, but the problem here is that the code I want to embrace into a div is all at the same level.



Thanks!!



<body>
<div class="article_text">
<div id="text">

<p class="paragraph">
<strong> [text] </strong>
</p>
<div>
<strong> [text] </strong>
</div>
<strong>
<br> [Title] <br>
</strong>
<div style="text-align: justify;">
<br> [text]
</div>
<div style="text-align: justify;">
<br> [text]
</div>

<span class="Title_Blue"> [text] </span>
<br>
<p> ... </p>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>

<span class="Title_Blue"> [text] </span>
<br>
<p> ... </p>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>

<span class="Title_Blue"> [text] </span>
<br>
<p> ... </p>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>

</div>
</div>
</body>


EDITED



I tried this:



$html = $data
$doc = new DOMDocument();
@$doc->loadHTML('<?xml encoding="UTF-8">' . $html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXpath($doc);
$contents = $xpath->query('//*[@class="Title_Blue"]');
foreach ($contents as $content) {
$div = $doc->createElement('div');
$div->setAttribute('class', 'DELETE_ME');
$content->parentNode->replaceChild($div, $content);
$div->appendChild($content);
}


EDITED 2



Was able to solve using this code:



$html = $data
$doc = new DOMDocument();
@$doc->loadHTML('<?xml encoding="UTF-8">' . $html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXpath($doc);
$contents = $xpath->query('//*[@class="Title_Blue"] | //*[@class="Title_Blue"]/following-sibling::*');
foreach ($contents as $content) {
$content->parentNode->removeChild($content);
}


Thanks to Andersson!










share|improve this question
















I have this html structure;
and I need to remove everything after



<span class="Title_Blue">


or, at least, put the block inside a div. (Then I can manipulate it.)



Is there anyway I can do it with xpath?



I can easily put a tag inside a div, but the problem here is that the code I want to embrace into a div is all at the same level.



Thanks!!



<body>
<div class="article_text">
<div id="text">

<p class="paragraph">
<strong> [text] </strong>
</p>
<div>
<strong> [text] </strong>
</div>
<strong>
<br> [Title] <br>
</strong>
<div style="text-align: justify;">
<br> [text]
</div>
<div style="text-align: justify;">
<br> [text]
</div>

<span class="Title_Blue"> [text] </span>
<br>
<p> ... </p>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>

<span class="Title_Blue"> [text] </span>
<br>
<p> ... </p>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>

<span class="Title_Blue"> [text] </span>
<br>
<p> ... </p>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>

</div>
</div>
</body>


EDITED



I tried this:



$html = $data
$doc = new DOMDocument();
@$doc->loadHTML('<?xml encoding="UTF-8">' . $html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXpath($doc);
$contents = $xpath->query('//*[@class="Title_Blue"]');
foreach ($contents as $content) {
$div = $doc->createElement('div');
$div->setAttribute('class', 'DELETE_ME');
$content->parentNode->replaceChild($div, $content);
$div->appendChild($content);
}


EDITED 2



Was able to solve using this code:



$html = $data
$doc = new DOMDocument();
@$doc->loadHTML('<?xml encoding="UTF-8">' . $html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXpath($doc);
$contents = $xpath->query('//*[@class="Title_Blue"] | //*[@class="Title_Blue"]/following-sibling::*');
foreach ($contents as $content) {
$content->parentNode->removeChild($content);
}


Thanks to Andersson!







php xpath






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 17:27







Migu3litto

















asked Nov 23 '18 at 17:02









Migu3littoMigu3litto

346




346













  • did you try anyting?

    – Madhur Sharma
    Nov 23 '18 at 17:03






  • 1





    You cannot use XPath to remove everything after, but you can use it to select everything after: //span[@class="Title_Blue"]/following-sibling::*

    – Andersson
    Nov 23 '18 at 17:11











  • Yes, it works! Thank you! Anyway I can include the original tag? Because it deletes everything "after". //*[@class="Title_Blue"]/following-sibling::* Maybe "and"

    – Migu3litto
    Nov 23 '18 at 17:18





















  • did you try anyting?

    – Madhur Sharma
    Nov 23 '18 at 17:03






  • 1





    You cannot use XPath to remove everything after, but you can use it to select everything after: //span[@class="Title_Blue"]/following-sibling::*

    – Andersson
    Nov 23 '18 at 17:11











  • Yes, it works! Thank you! Anyway I can include the original tag? Because it deletes everything "after". //*[@class="Title_Blue"]/following-sibling::* Maybe "and"

    – Migu3litto
    Nov 23 '18 at 17:18



















did you try anyting?

– Madhur Sharma
Nov 23 '18 at 17:03





did you try anyting?

– Madhur Sharma
Nov 23 '18 at 17:03




1




1





You cannot use XPath to remove everything after, but you can use it to select everything after: //span[@class="Title_Blue"]/following-sibling::*

– Andersson
Nov 23 '18 at 17:11





You cannot use XPath to remove everything after, but you can use it to select everything after: //span[@class="Title_Blue"]/following-sibling::*

– Andersson
Nov 23 '18 at 17:11













Yes, it works! Thank you! Anyway I can include the original tag? Because it deletes everything "after". //*[@class="Title_Blue"]/following-sibling::* Maybe "and"

– Migu3litto
Nov 23 '18 at 17:18







Yes, it works! Thank you! Anyway I can include the original tag? Because it deletes everything "after". //*[@class="Title_Blue"]/following-sibling::* Maybe "and"

– Migu3litto
Nov 23 '18 at 17:18














1 Answer
1






active

oldest

votes


















1














If you need to select everything after span including that span, try



//span[@class="Title_Blue"]/preceding-sibling::*[1]/following-sibling::*





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450558%2fphp-how-to-give-block-of-code-a-parent-node%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    If you need to select everything after span including that span, try



    //span[@class="Title_Blue"]/preceding-sibling::*[1]/following-sibling::*





    share|improve this answer




























      1














      If you need to select everything after span including that span, try



      //span[@class="Title_Blue"]/preceding-sibling::*[1]/following-sibling::*





      share|improve this answer


























        1












        1








        1







        If you need to select everything after span including that span, try



        //span[@class="Title_Blue"]/preceding-sibling::*[1]/following-sibling::*





        share|improve this answer













        If you need to select everything after span including that span, try



        //span[@class="Title_Blue"]/preceding-sibling::*[1]/following-sibling::*






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 17:35









        AnderssonAndersson

        39.1k113669




        39.1k113669
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450558%2fphp-how-to-give-block-of-code-a-parent-node%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini