I'm trying to get my PHP code to remove class="something" from links that contain the attribute data-class="false" and leave other links alone.
$mytext = 'text text text <a href="/url" data-class="false" class="something">
link1</a> text text text <a href="/url" class="something">link2</a> text text
text <a href="/url" class="something" data-class="false">link3</a> text text
text';
// This function is supposed to remove the class="something" string from matches.
function remove_class($matches) {
return str_replace(' class="something"','', $matches[1]);
}
// Match any link that contains the data-class="false" string and do callback.
$mytext = preg_replace_callback('/<a.*?data-class="false".*?>/',
'remove_class', $mytext);
echo $mytext;
Desired result below: (Notice the class is removed where data-class="false" exists)
text text text <a href="/url" data-class="false">link1</a> text text text
<a href="/url" class="something">link2</a> text text text
<a href="/url" data-class="false">link3</a> text text text