For the sake of anyone who may encounter this problem in the future, I’ll put the solution here:
I defined the matching regular expression (PCRE-compatible):
~<blockquote>\s*(.+?)<p class="entry-title">(.+?)<\/blockquote>~s
You can see it live at RegExr. Click “Explain” to understand the expression.
Then the replacement:
\1<p>\2
Then, here’s a test block with added surrounding content:
<blockquote>
<h3>Te sugerimos</h3>
<p class="entry-title"><a href="https://example.com/post-title/" target="_blank" rel="noopener noreferrer" style="outline: none;"><strong>POST TITLE</strong></a></p>
</blockquote>
<p>Other stuff</p>
<blockquote>Not matched</blockquote>
When the regex above is applied, for example as in preg_replace($pattern, $replace, $content)
, the above block transforms into:
<h3>Te sugerimos</h3>
<p><a href="https://example.com/post-title/" target="_blank" rel="noopener noreferrer" style="outline: none;"><strong>POST TITLE</strong></a></p>
<p>Other stuff</p>
<blockquote>Not matched</blockquote>
Which I the desired output.
To apply all this to the content there’s three basic options:
- Use MySQL’s REGEXP_REPLACE function – whether in terminal, in PHPMyAdmin, or from a PHP script. Check How to do a regular expression replace in MySQL? for usage examples, then match to your database structure.
- Handle the cleanup in PHP: Run a select query for all posts with this pattern; then modify content with
preg_replace
. Finally update the database entries.
- Download a database dump, open it up in your favorite text editor (with regex support), or pipe it into your tool of choice, and do the necessary replacements; finally reload into your database.
Whichever way you choose to do this, be sure to backup your data first.
Thanks.