I'm writing a plugin for Virtuemart its System type plugin. In their older version (VM2.x) they managed the Div
simply with a class and there is no inside Div
structure so preg_replace
is sufficient for my requirement. But on new version they changed the default Div structure now Div
inside Div
is there. So preg_replace
is unable to replace the whole Div
content.
I just used to manage it with PHPDom
and Xpath
but in some template frameworks (T3)
when my plugin enable return an error.
"0 - String could not be parsed as XML"
the same plugin works fine with Gantry
and other frameworks.
The old version I tried the following code and its works fine on all templates.
$buffer = preg_replace('<div class="ClassNeedstoReplace">([^`]*?)<\/div>/','my custom content',$buffer);
Now in Vm3 I'm using following codes.
$docs = new DOMDocument();
libxml_use_internal_errors(true);
$docs->resolveExternals = true;
$docs->loadHTML($buffer);
if($getRelatedProduct != '')
$element = $docs->createElement('div', $getRelatedProduct);
$xpath = new DOMXPath($docs);
$query = '//div[@class="'.$class_name.'"]';
$entries = $xpath->query($query);
foreach ($entries as $entry) {
$entry->parentNode->replaceChild($element, $entry);
}
$docs->recover = true;
libxml_clear_errors();
JResponse::setBody(htmlspecialchars_decode($docs->saveHTML()));
bcoz all the instance of the class needs to be replaced.
I know for testing the plugin required another Extension (VM3).its difficult for debuggers.But simply my requirement is simply replace all the class instance with mine.
is there any way to replace Div inside Div structure without breaking layouts using preg_replace
??
The Div Structure is
<div class="product-fields">
<div class="product-field product-field-type-R">
<span class="product-fields-title-wrapper"><span class="product-fields-title"><strong>Related Products</strong></span>
<span title="" class="hasTooltip" data-original-title="<strong>Related Products</strong><br />COM_VIRTUEMART_RELATED_PRODUCTS_TIP"><img alt="Tooltip" src="/j34/media/system/images/tooltip.png"></span></span>
<span class="product-field-display"><a target="blank" title="Test Product 1" href="/j34/index.php/shops/c1/t1-detail">Test Product 1</a></span><span class="product-field-desc">Custom prototype for related products</span> </div>
</div>
I'm trying to replace "product-field product-field-type-R
" it may comes more than one inside "product-fields
"
Hope someone will be able to figure out something :)