-1

Html Markup

    <div class="entry-content entry-excerpt clearfix">
        <div class="simplesocialbuttons simplesocial-round-icon simplesocialbuttons_inline simplesocialbuttons-align-centered post-1 post  simplesocialbuttons-inline-no-animation simplesocialbuttons-inline-in">
<button class="simplesocial-fb-share" target="_blank" data-href="https://www.facebook.com/sharer/sharer.php?u=http://localhost/wp/hello-world/" onclick="javascript:window.open(this.dataset.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;"><span class="simplesocialtxt">Facebook </span> </button>
<button class="simplesocial-msng-share" onclick="javascript:window.open( this.dataset.href, '_blank',  'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600' );return false;" data-href="http://www.facebook.com/dialog/send?app_id=891268654262273&amp;redirect_uri=http%3A%2F%2Flocalhost%2Fwp%2Fhello-world%2F&amp;link=http%3A%2F%2Flocalhost%2Fwp%2Fhello-world%2F&amp;display=popup"><span class="simplesocialtxt">Messenger</span></button> 
<button onclick="javascript:window.open(this.dataset.href, '_blank' );return false;" class="simplesocial-whatsapp-share" data-href="https://api.whatsapp.com/send?text=http://localhost/wp/hello-world/"><span class="simplesocialtxt">WhatsApp</span></button>
<button class="simplesocial-tumblr-share" data-href="http://tumblr.com/widgets/share/tool?canonicalUrl=http%3A%2F%2Flocalhost%2Fwp%2Fhello-world%2F" onclick="javascript:window.open(this.dataset.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;"><span class="simplesocialtxt">Tumblr</span> </button>
<button class="simplesocial-twt-share" data-href="https://twitter.com/share?text=Hello+world%21&amp;url=http://localhost/wp/hello-world/" rel="nofollow" onclick="javascript:window.open(this.dataset.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;"><span class="simplesocialtxt">Twitter</span> </button>
<button onclick="javascript:window.location.href = this.dataset.href;return false;" class="simplesocial-email-share" data-href="mailto:?subject=Hello+world%21&amp;body=http://localhost/wp/hello-world/"><span class="simplesocialtxt">Email</span></button>
<button class="simplesocial-gplus-share" data-href="https://plus.google.com/share?url=http://localhost/wp/hello-world/" onclick="javascript:window.open(this.dataset.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;"><span class="simplesocialtxt">Google+</span></button>
<button target="popup" class="simplesocial-linkedin-share" data-href="https://www.linkedin.com/cws/share?url=http://localhost/wp/hello-world/" onclick="javascript:window.open(this.dataset.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;"><span class="simplesocialtxt">LinkedIn</span></button>
<button rel="nofollow" class="simplesocial-pinterest-share" onclick="var e=document.createElement('script');e.setAttribute('type','text/javascript');e.setAttribute('charset','UTF-8');e.setAttribute('src','//assets.pinterest.com/js/pinmarklet.js?r='+Math.random()*99999999);document.body.appendChild(e);return false;"><span class="simplesocialtxt">Pinterest</span></button>
<button class="simplesocial-reddit-share" data-href="https://reddit.com/submit?url=http://localhost/wp/hello-world/&amp;title=Hello+world%21" onclick="javascript:window.open(this.dataset.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;"><span class="simplesocialtxt">Reddit</span> </button>
</div>

<p>Wel&shy;come to Word&shy;Press.<br>
<img class="alignnone size-medium wp-image-8" src="http://localhost/wp/wp-content/uploads/2018/07/DZV_1UkX4AEUlTZ-300x202.jpg" alt="" width="300" height="202" srcset="http://localhost/wp/wp-content/uploads/2018/07/DZV_1UkX4AEUlTZ-300x202.jpg 300w, http://localhost/wp/wp-content/uploads/2018/07/DZV_1UkX4AEUlTZ-768x517.jpg 768w, http://localhost/wp/wp-content/uploads/2018/07/DZV_1UkX4AEUlTZ.jpg 789w" sizes="(max-width: 300px) 100vw, 300px"><br>
This is your first post. Edit or delete it, then start writ&shy;ing!</p>

            <a href="http://localhost/wp/hello-world/" class="more-link">Read more</a>

            </div>

This is my php:

preg_match_all( '/<[^>]*class="[^"]*\bsimplesocialbuttons\b[^"]*"[^>]*>/', $original_text, $matches );

My current result:

<div class="simplesocialbuttons simplesocial-round-icon  simplesocialbuttons_inline simplesocialbuttons-align-centered post-1 post   simplesocialbuttons-inline-no-animation simplesocialbuttons-inline-in">

My desired result:

<div class="simplesocialbuttons simplesocial-round-icon  simplesocialbuttons_inline simplesocialbuttons-align-centered post-1 post   simplesocialbuttons-inline-no-animation simplesocialbuttons-inline-in">
all content inside div
</div>

I also tried to search for a solution, but did not find any correct solutions.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • I find this generalist dupe closure to be a bit rude (it is a "go away closure"). I am happy to see this dupe used to squash no-effort questions, but this question shows effort. Surely a better dupe can be found. – mickmackusa Sep 01 '18 at 10:30
  • if you wanted to remove a portion of the dom, you should have clarified this point in your question (and used `preg_replace()` to indicate the process in your coding attempt). – mickmackusa Sep 01 '18 at 10:32
  • @Wiktor this is a more suitable/informative duplicate for what the OP wants to do: https://stackoverflow.com/a/27354151/2943403 – mickmackusa Sep 02 '18 at 00:05

1 Answers1

0

Using DomDocument and Xpath makes your process very stable and accurate. Even if the html structure is altered slightly in the future, you stand the best chance of maintaining the desired output versus regex.

Query Breakdown:

//                                            #from any level in the document
div                                           #match any div tag
[contains(@class, 'simplesocialbuttons')]     #which has a class called simplesocialbuttons

Once you have isolated the targeted node, $dom->saveHTML($node) will grab the div and the "innerHTML" that you are seeking.

Code: (Demo)

$html = <<<HTML
<div class="entry-content entry-excerpt clearfix">
        <div class="simplesocialbuttons simplesocial-round-icon simplesocialbuttons_inline simplesocialbuttons-align-centered post-1 post  simplesocialbuttons-inline-no-animation simplesocialbuttons-inline-in">
<button class="simplesocial-fb-share" target="_blank" data-href="https://www.facebook.com/sharer/sharer.php?u=http://localhost/wp/hello-world/" onclick="javascript:window.open(this.dataset.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;"><span class="simplesocialtxt">Facebook </span> </button>
<button class="simplesocial-msng-share" onclick="javascript:window.open( this.dataset.href, '_blank',  'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600' );return false;" data-href="http://www.facebook.com/dialog/send?app_id=891268654262273&amp;redirect_uri=http%3A%2F%2Flocalhost%2Fwp%2Fhello-world%2F&amp;link=http%3A%2F%2Flocalhost%2Fwp%2Fhello-world%2F&amp;display=popup"><span class="simplesocialtxt">Messenger</span></button> 
<button onclick="javascript:window.open(this.dataset.href, '_blank' );return false;" class="simplesocial-whatsapp-share" data-href="https://api.whatsapp.com/send?text=http://localhost/wp/hello-world/"><span class="simplesocialtxt">WhatsApp</span></button>
<button class="simplesocial-tumblr-share" data-href="http://tumblr.com/widgets/share/tool?canonicalUrl=http%3A%2F%2Flocalhost%2Fwp%2Fhello-world%2F" onclick="javascript:window.open(this.dataset.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;"><span class="simplesocialtxt">Tumblr</span> </button>
<button class="simplesocial-twt-share" data-href="https://twitter.com/share?text=Hello+world%21&amp;url=http://localhost/wp/hello-world/" rel="nofollow" onclick="javascript:window.open(this.dataset.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;"><span class="simplesocialtxt">Twitter</span> </button>
<button onclick="javascript:window.location.href = this.dataset.href;return false;" class="simplesocial-email-share" data-href="mailto:?subject=Hello+world%21&amp;body=http://localhost/wp/hello-world/"><span class="simplesocialtxt">Email</span></button>
<button class="simplesocial-gplus-share" data-href="https://plus.google.com/share?url=http://localhost/wp/hello-world/" onclick="javascript:window.open(this.dataset.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;"><span class="simplesocialtxt">Google+</span></button>
<button target="popup" class="simplesocial-linkedin-share" data-href="https://www.linkedin.com/cws/share?url=http://localhost/wp/hello-world/" onclick="javascript:window.open(this.dataset.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;"><span class="simplesocialtxt">LinkedIn</span></button>
<button rel="nofollow" class="simplesocial-pinterest-share" onclick="var e=document.createElement('script');e.setAttribute('type','text/javascript');e.setAttribute('charset','UTF-8');e.setAttribute('src','//assets.pinterest.com/js/pinmarklet.js?r='+Math.random()*99999999);document.body.appendChild(e);return false;"><span class="simplesocialtxt">Pinterest</span></button>
<button class="simplesocial-reddit-share" data-href="https://reddit.com/submit?url=http://localhost/wp/hello-world/&amp;title=Hello+world%21" onclick="javascript:window.open(this.dataset.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;"><span class="simplesocialtxt">Reddit</span> </button>
</div>

<p>Wel&shy;come to Word&shy;Press.<br>
<img class="alignnone size-medium wp-image-8" src="http://localhost/wp/wp-content/uploads/2018/07/DZV_1UkX4AEUlTZ-300x202.jpg" alt="" width="300" height="202" srcset="http://localhost/wp/wp-content/uploads/2018/07/DZV_1UkX4AEUlTZ-300x202.jpg 300w, http://localhost/wp/wp-content/uploads/2018/07/DZV_1UkX4AEUlTZ-768x517.jpg 768w, http://localhost/wp/wp-content/uploads/2018/07/DZV_1UkX4AEUlTZ.jpg 789w" sizes="(max-width: 300px) 100vw, 300px"><br>
This is your first post. Edit or delete it, then start writ&shy;ing!</p>

            <a href="http://localhost/wp/hello-world/" class="more-link">Read more</a>

            </div>
HTML;

$dom = new DOMDocument; 
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
// using a loop in case there are multiple occurrences
foreach ($xpath->query("//div[contains(@class, 'simplesocialbuttons')]") as $node) {
  $result[] = $dom->saveHTML($node);
}
var_export($result);

Output:

array (
  0 => '<div class="simplesocialbuttons simplesocial-round-icon simplesocialbuttons_inline simplesocialbuttons-align-centered post-1 post  simplesocialbuttons-inline-no-animation simplesocialbuttons-inline-in">
<button class="simplesocial-fb-share" target="_blank" data-href="https://www.facebook.com/sharer/sharer.php?u=http://localhost/wp/hello-world/" onclick="javascript:window.open(this.dataset.href, \'\', \'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600\');return false;"><span class="simplesocialtxt">Facebook </span> </button>
<button class="simplesocial-msng-share" onclick="javascript:window.open( this.dataset.href, \'_blank\',  \'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600\' );return false;" data-href="http://www.facebook.com/dialog/send?app_id=891268654262273&amp;redirect_uri=http%3A%2F%2Flocalhost%2Fwp%2Fhello-world%2F&amp;link=http%3A%2F%2Flocalhost%2Fwp%2Fhello-world%2F&amp;display=popup"><span class="simplesocialtxt">Messenger</span></button> 
<button onclick="javascript:window.open(this.dataset.href, \'_blank\' );return false;" class="simplesocial-whatsapp-share" data-href="https://api.whatsapp.com/send?text=http://localhost/wp/hello-world/"><span class="simplesocialtxt">WhatsApp</span></button>
<button class="simplesocial-tumblr-share" data-href="http://tumblr.com/widgets/share/tool?canonicalUrl=http%3A%2F%2Flocalhost%2Fwp%2Fhello-world%2F" onclick="javascript:window.open(this.dataset.href, \'\', \'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600\');return false;"><span class="simplesocialtxt">Tumblr</span> </button>
<button class="simplesocial-twt-share" data-href="https://twitter.com/share?text=Hello+world%21&amp;url=http://localhost/wp/hello-world/" rel="nofollow" onclick="javascript:window.open(this.dataset.href, \'\', \'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600\');return false;"><span class="simplesocialtxt">Twitter</span> </button>
<button onclick="javascript:window.location.href = this.dataset.href;return false;" class="simplesocial-email-share" data-href="mailto:?subject=Hello+world%21&amp;body=http://localhost/wp/hello-world/"><span class="simplesocialtxt">Email</span></button>
<button class="simplesocial-gplus-share" data-href="https://plus.google.com/share?url=http://localhost/wp/hello-world/" onclick="javascript:window.open(this.dataset.href, \'\', \'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600\');return false;"><span class="simplesocialtxt">Google+</span></button>
<button target="popup" class="simplesocial-linkedin-share" data-href="https://www.linkedin.com/cws/share?url=http://localhost/wp/hello-world/" onclick="javascript:window.open(this.dataset.href, \'\', \'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600\');return false;"><span class="simplesocialtxt">LinkedIn</span></button>
<button rel="nofollow" class="simplesocial-pinterest-share" onclick="var e=document.createElement(\'script\');e.setAttribute(\'type\',\'text/javascript\');e.setAttribute(\'charset\',\'UTF-8\');e.setAttribute(\'src\',\'//assets.pinterest.com/js/pinmarklet.js?r=\'+Math.random()*99999999);document.body.appendChild(e);return false;"><span class="simplesocialtxt">Pinterest</span></button>
<button class="simplesocial-reddit-share" data-href="https://reddit.com/submit?url=http://localhost/wp/hello-world/&amp;title=Hello+world%21" onclick="javascript:window.open(this.dataset.href, \'\', \'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600\');return false;"><span class="simplesocialtxt">Reddit</span> </button>
</div>',
)

EDIT: To remove the targeted node and its innerHTML...

Code: (Demo)

foreach ($xpath->query("//div[contains(@class, 'simplesocialbuttons')]") as $node) {
    $node->parentNode->removeChild($node);
}
echo $dom->saveHTML();

Output:

<div class="entry-content entry-excerpt clearfix">


<p>Wel&shy;come to Word&shy;Press.<br>
<img class="alignnone size-medium wp-image-8" src="http://localhost/wp/wp-content/uploads/2018/07/DZV_1UkX4AEUlTZ-300x202.jpg" alt="" width="300" height="202" srcset="http://localhost/wp/wp-content/uploads/2018/07/DZV_1UkX4AEUlTZ-300x202.jpg 300w, http://localhost/wp/wp-content/uploads/2018/07/DZV_1UkX4AEUlTZ-768x517.jpg 768w, http://localhost/wp/wp-content/uploads/2018/07/DZV_1UkX4AEUlTZ.jpg 789w" sizes="(max-width: 300px) 100vw, 300px"><br>
This is your first post. Edit or delete it, then start writ&shy;ing!</p>

            <a href="http://localhost/wp/hello-world/" class="more-link">Read more</a>

            </div>
mickmackusa
  • 43,625
  • 12
  • 83
  • 136