As always I've trouble writing a great regex.

I'm attempting to make a wordpress plugin for Joomla to include a control button towards the optional print, email and PDF buttons created through the core around the right of article game titles. Basically succeed I'll distribute it underneath the GPL. No good examples I discovered appear to operate and I must produce a php-only solution.

The concept is by using the initial pattern from the Joomla output for article game titles and buttons for a number of regex. One regex would find the correct table by searching for a table with class "contentpaneopen" (which you will find several inside a page) and that contains a cell with class "contentheading". Another regex could see if for the reason that table there's a cell with class "buttonheading". The amount of these cells might be from zero to 3 however i can use this see if the very first regex returns several match. With this particular, I must replace the table through the same table however with an additional cell holding the button I wish to add. I possibly could do this if you take from the last row and table closing tags and placing my button cell before adding individuals closing tags again.

The standard Joomla output appears like this:

<table class="contentpaneopen">
    		<td width="100%" class="contentheading">
    			<a class="contentpagetitle" href="url">Title Here</a>
    		<td width="100%" align="right" class="buttonheading">
    			<a rel="nofollow" onclick="etc" title="PDF" href="url"><img alt="PDF" src="/templates/neutral/images/pdf_button.png"/></a>
    		<td width="100%" align="right" class="buttonheading">
    			<a rel="nofollow" onclick="etc" title="Print" href="url"><img alt="Print" src="/templates/neutral/images/printButton.png" ></a>

The code would very roughly be something similar to this:

$subject = $article;
$pattern1 = '[regex1]'; //<table class="contentpaneopen">etc</table>
preg_match($pattern, $subject, $match);
$pattern2 = '[regex2]'; //</tr></tbody></table>
$replacement = [mybutton];
echo preg_replace($pattern2, $replacement, $match);

With no good regex there's little point doing the relaxation from the code, and so i hope someone can sort out that!

This can be a common query on SO and the reply is always exactly the same: regular expressions really are a poor option for parsing or processing HTML or XML. You will find many different ways they are able to break lower. PHP includes a minimum of three built-in HTML parsers that'll be much more robust.

Have a look at Parse HTML With PHP And DOM and employ something similar to:

$html = new DomDocument;
$html->preserveWhiteSpace = false; 
$tables = $html->getElementsByTagName('table'); 
foreach ($tables as $table) {
  if ($table->getAttribute('class') == 'contentpaneopen') {
    // replace it with something else

It is possible to reason you need to use regex with this? DOM parsing could be a lot more straightforward.