Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete divs from html source code [closed]

I have html code in which are many divs, and want delete some divs from this code, it's so hard to do with substr() or similar php string function, maybe are better way to do this ?

like image 612
Wizard Avatar asked Dec 08 '22 21:12

Wizard


2 Answers

strip_tags() might be of help:

http://php.net/manual/en/function.strip-tags.php

like image 162
Miroslav Avatar answered Dec 28 '22 21:12

Miroslav


You can do this with javascript. Just give the divs unique id's, then you can write some simple javascript like so.

 document.getElementById('divid').remove();

Note: Make sure the element(s) have loaded before running the remove(). - You can do this with jquery.

$(document).ready(function(){
     document.getElementById('divid').remove();
});

Alertnativly, if you decide to add elements maybe some time after the document has loaded, then remove them, you can check to see if an element exists with the following code. (again using the jquery library), could can also use css selectors with an object, being as you're using jquery.

if ($("#divid").length > 0){
    $('#divid').remove();
}

That code can become handy, if say - You gradually add elements into the document, with like a live-comment system, you could use setInterval(), to keep checking if the element exists, and when it does, remove it.


If you NEED to do this with php then Brian Agnew's answer of using the PHP HTML parser should solve your issue.

like image 20
Jordan Richards Avatar answered Dec 28 '22 20:12

Jordan Richards