0

I'm looking to do the opposite of this.

<?php
// get the content
$block = get_the_content();

// check and retrieve blockquote
if(preg_match('~<blockquote>([\s\S]+?)</blockquote>~', $block, $matches))

// output blockquote
echo '<p><span>'.$matches[1].'</span></p>';
?>

How to show the content outside of the blockquote.

bobble bubble
  • 16,888
  • 3
  • 27
  • 46
user3550879
  • 3,389
  • 6
  • 33
  • 62
  • whats your real question ? – Blueblazer172 Dec 09 '16 at 13:00
  • I'm looking to do the opposite of what the code currently does, which is post just the blockquotes from the content. I want to post the content without the blockquotes. – user3550879 Dec 09 '16 at 13:04
  • add a `^` before the pattern. that will negate it – Blueblazer172 Dec 09 '16 at 13:05
  • will that get the content from the post WITHOUT the blockquote? – user3550879 Dec 11 '16 at 05:51
  • @user3550879 Please check if I did edit your question correctly. You need to match everything outside the `
    `...`
    ` right?
    – bobble bubble Dec 11 '16 at 10:59
  • http://stackoverflow.com/a/1732454/477127 You can't reliably parse markup with regex – GordonM Dec 11 '16 at 11:00
  • @user3550879 I leave my answer as a comment. How about use of `preg_split` which default will discard the split-sequence and return what's left. See [this demo at eval.in `$res = preg_split('~
    .*?
    ~s', $str);`](https://eval.in/695053).
    – bobble bubble Dec 11 '16 at 11:28
  • I'm just looking to take out the
    from my wordpress post and post everything else to the page. I can only control what is and isn't a blockquote from the admin side of wordpress
    – user3550879 Dec 11 '16 at 19:27

1 Answers1

2

You really shouldn't use regular expression for HTML-parsing. I recommend using phpQuery to solve your problem and any other similar problems in the future. phpQuery is working like jQuery to select elements in HTML and to modify them. In phpQuery you can just do this:

$markup = '<div><span>Hello</span><blockquote>Remove me!</blockquote>World<div/>';
$doc = phpQuery::newDocumentHTML($markup);
$doc['blockquote']->remove();
echo $doc;

So you load your HTML content to phpQuery, select the blockquote, remove it, and print out the changed string.

If you still insist to do it with regex, here it is:

$block = preg_replace('~<blockquote>([\s\S]+?)</blockquote>~', '', $block);
echo $block;
Community
  • 1
  • 1
Martin Cup
  • 2,399
  • 1
  • 21
  • 32
  • Ok, I havent tried that route before. I don't have control of the content just what is and isn't in
    tags. Reading your $markup line just has me a bit confused
    – user3550879 Dec 11 '16 at 19:25
  • The post content is created in the Wordpress admin panel by the user. So I only have limited control – user3550879 Dec 11 '16 at 19:31