Skip to content

Instantly share code, notes, and snippets.

@basicsharp
Last active December 13, 2015 23:48
Show Gist options
  • Select an option

  • Save basicsharp/4993747 to your computer and use it in GitHub Desktop.

Select an option

Save basicsharp/4993747 to your computer and use it in GitHub Desktop.
^^
<?php
function replace_class($input, $except_classes=false) {
if (!$except_classes) {
return preg_replace('/ class=".*?"/', '', $input);
}
preg_match("/(class=\")(.*?)(\")/um", $input, $matches);
$classes = explode(' ', $matches[2]);
$filtered_classes = false;
foreach ($classes as $class) {
if (in_array($class, $except_classes))
$filtered_classes = ' '.$class;
}
if ($filtered_classes) {
if (strlen($filtered_classes) > 1)
$filtered_classes = substr($filtered_classes, 1);
return preg_replace('/ class=".*?"/', ' class="'.$filtered_classes.'"', $input);
}
else {
return preg_replace('/ class=".*?"/', '', $input);
}
}
$input = '<a class="xxx yyy zzz" href="">';
echo replace_class($input, array('yyy'));
//output: <a class="yyy" href="">
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment