1

I have the following 2 sets of code (Wordpress) using regex, but I was told that it's a bad practice.
I am using it in 2 ways:

  1. To ake out the blockquote and images from the post and just display the text.
  2. To essentially do the opposite and display just the images.

Looking to write it in the proper more acceptable/cross browser form.

html (display text):

<?php
$content = preg_replace('/<blockquote>(.*?)<\/blockquote>/', '', get_the_content());
$content = preg_replace('/(<img [^>]*>)/', '', $content);
$content = wpautop($content); // Add paragraph-tags
$content = str_replace('<p></p>', '', $content); // remove empty paragraphs
echo $content;
?>  

html (display images):

<?php
preg_match_all('/(<img [^>]*>)/', get_the_content(), $images);
for( $i=0; isset($images[1]) && $i < count($images[1]); $i++ ) {
    if ($i == end(array_keys($images[1]))) {
        echo sprintf('<div id="last-img">%s</div>', $images[1][$i]);
        continue;
    }
    echo $images[1][$i];
}
?>
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
user3550879
  • 3,389
  • 6
  • 33
  • 62

1 Answers1

0

You can use the answer from here: Strip Tags and everything in between

The point is to use a parser, rather than roll-your-own regex that might be buggy.

$content = get_the_content();
$content = wpautop($content);

$doc = new DOMDocument();
$doc->loadHTML(get_the_content(), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

$xpath = new DOMXPath($doc);

foreach ($xpath->query('//blockquote') as $node) {
    $node->parentNode->removeChild($node);
}

foreach ($xpath->query('//img') as $node) {
    $node->parentNode->removeChild($node);
}

foreach( $xpath->query('//p[not(node())]') as $node ) {
    $node->parentNode->removeChild($node);
}

$content = $doc->saveHTML($doc);

You may find that php DOMDocument has wrapped your html fragment in <html> tags, in which case look at How to saveHTML of DOMDocument without HTML wrapper?

The part that removes empty p tags is from Remove empty tags from a XML with PHP

Community
  • 1
  • 1
Chris Lear
  • 6,592
  • 1
  • 18
  • 26
  • How would I place each piece back into the page html after it's taken out? in different places? I used echo in my previous code – user3550879 Jun 03 '16 at 23:45
  • Just use `echo` again. Try `echo $content` at the end of the code above – Chris Lear Jun 06 '16 at 08:21
  • wouldn't that put it all back? – user3550879 Jun 06 '16 at 15:30
  • I'm not sure what you're asking. `$content` should contain an altered version of the original content. Have you tried running this? – Chris Lear Jun 06 '16 at 15:37
  • I apologize I mean I want to be able to put each piece that has been removed back in different spots. So the content would be put with the altered parts. But how'd I put the removed elements back – user3550879 Jun 06 '16 at 15:40
  • Hmm. Your original question shows the blockquotes and img tags being removed altogether, and empty p tags being removed. My version only replicates that. – Chris Lear Jun 06 '16 at 15:44
  • Oh - I see what you're asking. You want help with the "(display images)" segment of your question. I'm afraid you're on your own, but the documentation (http://php.net/manual/en/class.domdocument.php) should help. – Chris Lear Jun 06 '16 at 15:47