Search code examples
phpregexstr-replacepreg-replace-callback

Remove a string from regex match results (remove link class if link contains a specific attribute)


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

Solution

  • From the code you are showing I am not seeing a reason to use preg_replace_callback, preg_replace should be sufficient.

    $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';
    
    // Match any link that contains the data-class="false" and class="something" 
    // Then removes class="something".
    $mytext = preg_replace('/(<a.*?)(class="something")\s(.*?data-class="false".*?>)|(<a.*?)(data-class="false".*?)\s(class="something")(.*?>)/', 
    '$1$3$4$5$7', $mytext);
    
    echo $mytext;
    

    Will Output:

    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
    

    Whats going on is that preg_replace matches class="something" data-class="false" OR data-class="false" class="something". Each sub-pattern (...) can be referenced by $ and number of the sub-pattern. If our first three sub-patterns are found then we use $1$3 leaving out $2, replacing the match with only the sub-pattern matches that we want. Since sub-patterns $4-$7 our not used they are ignored and vise versa if we match $4-$7. By leaving \s out of the sub-pattern we will be getting rid of and extra space.