I want to try replace all instances of the following tags and remove the p and end p before and after but only when the the class readmore is used
<p><a class="readmore" href="http://www.google.com">My External Link</a></p>
Thanks in advance.
I want to try replace all instances of the following tags and remove the p and end p before and after but only when the the class readmore is used
<p><a class="readmore" href="http://www.google.com">My External Link</a></p>
Thanks in advance.
You've asked for regex, but it's not really a good solution for parsing HTML.
Load the document you want to process, find the elements you want to shuffle, shuffle them. Something not unlike this might work:
$document = new DOMDocument();
$document->loadHTMLFile(FILENAME);
$xpath = new DOMXPath($document);
$nodeList = $xpath->evaluate("//p[contains(a[@class='readmore'])]");
foreach ($nodeList as $node) {
$node->parentNode->replaceChild($node->firstChild, $node);
}
See this answer for help removing nodes with an XSL stylesheet. You can build a template something like the following:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p[contains(a[@class='readmore'])]">
<xsl:copy-of select="a"/>
</xsl:template>
</xsl:stylesheet>
If you really want to go the way of doom then I don't suppose I'll be able to stop you. Make sure you've got everything in source control and review the diffs before committing...
preg_replace('#<p>(<a class="readmore" href="[^"]+">[^<]*<\/a>)<\/p>#', '\1');