Remove Div will all content

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
YoussefSiblini
Forum Contributor
Posts: 206
Joined: Thu Jul 21, 2011 1:51 pm

Remove Div will all content

Post by YoussefSiblini »

Hi,

I want to remove all divs with class "itp-share" and there contents, here is one div and it's content:

Code: Select all

<div class="itp-share">
             	<div class="itp-share-tw">
                	<iframe scrolling="no" frameborder="0" allowtransparency="true" src="http://platform.twitter.com/widgets/tweet_button.1363148939.html#_=1365061661022&count=horizontal&id=twitter-widget-0&lang=en&original_referer=http%3A%2F%2Fwww.mygreatmove.com%2Findex.php%2Fblog&size=m&text=Where%20to%20Live%20in%20London&url=http%3A%2F%2Fwww.mygreatmove.com%2Findex.php%2Fblog%2Fitem%2F31-where-to-live-in-london" class="twitter-share-button twitter-count-horizontal" style="width: 109px; height: 20px;" title="Twitter Tweet Button" data-twttr-rendered="true"></iframe><script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script></div>
            <div class="itp-share-lin"><script src="//platform.linkedin.com/in.js"></script><span style="line-height: 1; vertical-align: baseline; display: inline-block; text-align: center;" class="IN-widget"><span style="padding: 0px ! important; margin: 0px ! important; text-indent: 0px ! important; display: inline-block ! important; vertical-align: baseline ! important; font-size: 1px ! important;"><span id="li_ui_li_gen_1365061661227_0"><a href="javascript:void(0);" id="li_ui_li_gen_1365061661227_0-link"><span id="li_ui_li_gen_1365061661227_0-logo">in</span><span id="li_ui_li_gen_1365061661227_0-title"><span id="li_ui_li_gen_1365061661227_0-mark"></span><span id="li_ui_li_gen_1365061661227_0-title-text">Share</span></span></a></span></span><span style="padding: 0px ! important; margin: 0px ! important; text-indent: 0px ! important; display: inline-block ! important; vertical-align: baseline ! important; font-size: 1px ! important;"><span class="IN-right IN-hidden" id="li_ui_li_gen_1365061661256_1-container"><span class="IN-right" id="li_ui_li_gen_1365061661256_1"><span class="IN-right" id="li_ui_li_gen_1365061661256_1-inner"><span class="IN-right" id="li_ui_li_gen_1365061661256_1-content">0</span></span></span></span></span></span><script data-counter="right" data-url="http://www.mygreatmove.com/index.php/blog/item/31-where-to-live-in-london" type="IN/Share+init"></script>
            </div>
            <div class="itp-share-fbl"><div id="fb-root" class=" fb_reset"><div style="position: absolute; top: -10000px; height: 0px; width: 0px;"><div><iframe scrolling="no" frameborder="0" name="fb_xdm_frame_http" allowtransparency="true" id="fb_xdm_frame_http" aria-hidden="true" title="Facebook Cross Domain Communication Frame" tab-index="-1" style="border: medium none;" src="http://static.ak.facebook.com/connect/xd_arbiter.php?version=21#channel=f3ed74b844b746e&origin=http%3A%2F%2Fwww.mygreatmove.com&channel_path=%2Findex.php%2Fblog%3Ffb_xd_fragment%23xd_sig%3Df29b5b0ecafda42%26"></iframe><iframe scrolling="no" frameborder="0" name="fb_xdm_frame_https" allowtransparency="true" id="fb_xdm_frame_https" aria-hidden="true" title="Facebook Cross Domain Communication Frame" tab-index="-1" style="border: medium none;" src="https://s-static.ak.facebook.com/connect/xd_arbiter.php?version=21#channel=f3ed74b844b746e&origin=http%3A%2F%2Fwww.mygreatmove.com&channel_path=%2Findex.php%2Fblog%3Ffb_xd_fragment%23xd_sig%3Df29b5b0ecafda42%26"></iframe></div></div><div style="position: absolute; top: -10000px; height: 0px; width: 0px;"><div></div></div></div> 
<script>(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
            <div data-action="like" data-colorscheme="light" data-show-faces="false" data-width="90" data-layout="button_count" data-send="1" data-href="http://www.mygreatmove.com/index.php/blog/item/31-where-to-live-in-london" class="fb-like fb_edge_widget_with_comment fb_iframe_widget" fb-xfbml-state="rendered"><span style="height: 20px; width: 126px;"><iframe scrolling="no" id="f3f2d1648060336" name="f286c818dcc875c" style="border: medium none; overflow: hidden; height: 20px; width: 126px;" title="Like this content on Facebook." class="fb_ltr" src="http://www.facebook.com/plugins/like.php?api_key=&locale=en_US&sdk=joey&channel_url=http%3A%2F%2Fstatic.ak.facebook.com%2Fconnect%2Fxd_arbiter.php%3Fversion%3D21%23cb%3Df2ecebdfe4305a8%26origin%3Dhttp%253A%252F%252Fwww.mygreatmove.com%252Ff3ed74b844b746e%26domain%3Dwww.mygreatmove.com%26relation%3Dparent.parent&href=http%3A%2F%2Fwww.mygreatmove.com%2Findex.php%2Fblog%2Fitem%2F31-where-to-live-in-london&node_type=link&width=150&layout=button_count&colorscheme=light&action=like&show_faces=false&send=true&extended_social_context=false"></iframe></span></div></div><div class="itp-share-gone"><div style="text-indent: 0px; margin: 0px; padding: 0px; background: none repeat scroll 0% 0% transparent; border-style: none; float: none; line-height: normal; font-size: 1px; vertical-align: baseline; display: inline-block; width: 90px; height: 20px;" id="___plusone_0"><iframe width="100%" scrolling="no" frameborder="0" hspace="0" marginheight="0" marginwidth="0" style="position: static; top: 0px; width: 90px; margin: 0px; border-style: none; left: 0px; visibility: visible; height: 20px;" tabindex="0" vspace="0" id="I0_1365061661554" name="I0_1365061661554" src="https://plusone.google.com/_/+1/fastbutton?bsv&size=medium&hl=en&origin=http%3A%2F%2Fwww.mygreatmove.com&url=http%3A%2F%2Fwww.mygreatmove.com%2Findex.php%2Fblog%2Fitem%2F31-where-to-live-in-london&jsh=m%3B%2F_%2Fscs%2Fapps-static%2F_%2Fjs%2Fk%3Doz.gapi.en.CqNG-rHlV_Y.O%2Fm%3D__features__%2Fam%3DQQ%2Frt%3Dj%2Fd%3D1%2Frs%3DAItRSTPbm59ToxQmNYFYDQ9kkwqhmvcGVA#_methods=onPlusOne%2C_ready%2C_close%2C_open%2C_resizeMe%2C_renderstart%2Concircled%2Conload&id=I0_1365061661554&parent=http%3A%2F%2Fwww.mygreatmove.com&rpctoken=64674027" allowtransparency="true" data-gapiattached="true" title="+1"></iframe></div><script> window.___gcfg = {lang: "en"};
              (function() {
                var po = document.createElement("script"); po.type = "text/javascript"; po.async = true;
                po.src = "https://apis.google.com/js/plusone.js";
                var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(po, s);
              })();
            </script></div>
        </div>
Will I be able to do this in php?

Joe
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Remove Div will all content

Post by requinix »

Sure. Loading the source up into a DOMDocument would be the most obvious first step.
YoussefSiblini
Forum Contributor
Posts: 206
Joined: Thu Jul 21, 2011 1:51 pm

Re: Remove Div will all content

Post by YoussefSiblini »

Thanks for your reply,
At least I know where to start now, I tried this:

Code: Select all

$doc = new DOMDocument();
$file = "index.php";
$doc->loadHTMLFile($file);

$xpath = new DOMXpath($doc);
$elements = $xpath->query("*/div[@class='itp-share-tw']");

foreach($elements as $e){
    $doc->removeChild($e);
}
$doc->saveHTMLFile($file);
This is out putting this: Warning: DOMDocument::loadHTMLFile(): Misplaced DOCTYPE declaration in index.php, line: 14 in C:\wamp\www\test\index.php on line 4
I am not sure what path the $file need to have inside it, as the name of the current page I am working on is index.php and not index.html.

Joe
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Remove Div will all content

Post by requinix »

If that index.php contains any PHP code at all then this won't work.

Sounds like there's something more going on here. What's the problem you're trying to solve?
YoussefSiblini
Forum Contributor
Posts: 206
Joined: Thu Jul 21, 2011 1:51 pm

Re: Remove Div will all content

Post by YoussefSiblini »

yes index.php contains php code inside it.

let me give you more details:

I am using joomla and I am adding social buttons to my blog pages(k2 template) and I want to display the social buttons only in the blog pages, but they are displaying in all pages.
My plan is to do this is:
get the url of the current page, if it contains blog string do nothing (keep buttons), but if it doesn't contain blog string then take all the divs with class "itp-share" and there contents which contains the social buttons.

This is what I am using for the url:

Code: Select all

<?php 
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (false !== strpos($url,'blog')) {
    // contains blog so do nothing
} else {
    // not blog so delete the social buttons
}
?>
Hope this helped
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Remove Div will all content

Post by requinix »

That's a clever solution but it's not very effective.

Can't you just create a second template that's nearly identical to the first but without the buttons? Then use that for the non-blog pages?
Post Reply