Hi search the regex version of this code :
$val = '<label for="A" >A</label><label for="B"><div>X</div></label><label for="C"><div>Y</div></label>';
$firstLabelPos = strpos($val,'<label for="B">');
$sideB = substr($val,$firstLabelPos + strlen('<label for="B">') );
$firstLabelPos2 = strpos($sideB,'</label>');
$result = substr($val,0,$firstLabelPos-1)
.substr($sideB,0,$firstLabelPos2)
.substr($sideB,$firstLabelPos2+strlen('</label>'));
result : <label for="A" >A</label<div>X</div><label for="C"><div>Y</div></label>
The code find <label for="B">
and the closest </label>
. Make a new string with what was before, inside and after.
Its how to remove the label around a div by the "for" attribute.
Or you can say, how to remove the first occurrence of <label for="B">
and the closest occurrence of </label>
.
If you can also make sur that we dont touch what is inside the <label...</label>
(could be another label). That will be appreciate for further practice.