I have string with span tags. I need to remove all inside span class="reference" tag. I use following code for remove string part. Part data-code in string part may vary.
$string = 'Some text <span class="reference" data-code="Z22">Data code</span>';
$pattern = "|(?<=<span class=\"reference\" data-code=\"Z22\">)(.*?)(?=<\/span>)|";
$replace = '<a href=""> replaced </a>';
$matches = array();
preg_match_all($pattern, $string, $matches);
foreach ($matches[0] as $value) {
$string = str_replace($value, $replace, $string);
}
echo $string;
How set $pattern for this script if data-code="will be variable"