I need to do replace some text. For that I need a regexp pattern. Here the text is:
<div CLASS="CHAP_BM_CON">Outer <div CLASS="CHAP_BM_FIRST">DIV inner</div>
Outer ends here</div><div CLASS="CHAP_BM_FIRST">Separate DIV Even inner</div>
In the above paragraph we can see one nested DIV. I need reg exp pattern to satisfy the below output like this:
<div CLASS="CHAP_BM_CON">Outer <div CLASS="CHAP_BM_FIRST">DIV inner <!--CHAP_BM_FIRST--></div>
Outer ends here<!--CHAP_BM_CON--></div><div CLASS="CHAP_BM_FIRST">Separate DIV Even inner<!--CHAP_BM_FIRST--></div>
How can we write the regexp for this case?
I tried the below code ..
<?php
function parseTagsRecursive($input)
{
$regex = '/<div tagname="(.*?)" (.*?)>(.*?)<\/div>/si';
if (is_array($input)) {
$input = '<div tagname='.$input[1].' '.$input[2].'>'.$input[3].'<!--'.$input[1].'--></div>';
}
return preg_replace_callback($regex, 'parseTagsRecursive', $input);
}
$content = '<div CLASS="CHAP_BM_CON">Outer <div CLASS="CHAP_BM_FIRST">DIV inner</div>
Outer ends here</div><div CLASS="CHAP_BM_FIRST">Separate DIV Even inner</div';
echo $ver = parseTagsRecursive($content);