-2

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"

D. Vasiliev
  • 97
  • 1
  • 9

1 Answers1

0

The data-code is placed in a positive lookbehind (?<=. If you want to make the value variable you might read this answer about Variable-Width Lookbehind vs. Infinite-Width Lookbehind.

But it is not recommended to parse html with regex.

What you might do is use a library like PHP Simple HTML DOM Parser and modify your html.

For example:

include_once ("simple_html_dom.php");

$data = <<<DATA
Some text <span class="reference" data-code="Z22">Data code</span>
DATA;

$html = str_get_html($data);
$ret = $html->find("span[class=reference]");
foreach ($ret as $node) {
    $node->innertext = '';
}
echo $html->save();

That would give you:

Some text <span class="reference" data-code="Z22"></span>
The fourth bird
  • 96,715
  • 14
  • 35
  • 52