0

I have content which contains many paragraphs, but I want to dynamically remove the last one. The last paragraph follows a similar pattern with static text and dynamic text. It looks like this : "This story * dynamic text here* was inspired by *varying text here * "

The words "This story" and "was inspired by" don't change.

I would like to use preg_replace in php wordpress to programmatically remove that last paragraph. I need help about the proper pattern to use to find and select that text, so that it is replaced.

Here's a sample of the $content

<p>First paragraph of text</p>
<p>Second paragraph</p>    
<p>This story <a rel="nofollow" href="http://example.com/inspiration">My Inspiration</a> was inspired by <a rel="nofollow" href="http://example.com">NiceOne</a>.</p>

I tried this, but it deletes the whole content.

$pattern = '^.*was inspired by.*$' ;
$replacement = '';
$new_content = preg_replace($pattern, $replacement, $content);

Thanks.

  • At the very least you are missing the front and back `/` in your pattern. – Rasclatt May 26 '18 at 14:40
  • https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags You shouldn't use regular expressions to parse HTML. If you know exactly what you want to replace - just replace the string directly. If however the HTML is dynamic, you can't just expect to reliably parse it using regular expressions because HTML is not a regular language. – Alex May 26 '18 at 14:47

0 Answers0