Skip to content

Instantly share code, notes, and snippets.

@ramsesdelr
Last active June 28, 2024 23:07
Show Gist options
  • Select an option

  • Save ramsesdelr/5b935d17caa47d0e3ee9d8c19f166bd2 to your computer and use it in GitHub Desktop.

Select an option

Save ramsesdelr/5b935d17caa47d0e3ee9d8c19f166bd2 to your computer and use it in GitHub Desktop.
<?php
/**
* Based on the regex findings, we process their response and add the new class to the matching results.
*
* @param array $matches An array of matches.
* @param string The processed content.
*/
function add_extra_strong_class( $matches ) {
$tag = $matches[1];
$attributes = $matches[2];
// Check if any class attribute exists.
if ( preg_match( '/\\bclass\\s*=\\s*["\\"]([^"\\"]*)["\\"]/', $attributes, $class_match ) ) {
$classes = explode( ' ', $class_match[1] );
// Check if "extra-strong" class already exists.
if ( ! in_array( 'extra-strong', $classes, true ) ) {
$classes[] = 'extra-strong';
}
$attributes = str_replace( $class_match[0], 'class="' . implode( ' ', $classes ) . '"', $attributes );
} else {
// If no class attribute exists, add the entire class attribute.
$attributes .= ' class="extra-strong"';
}
return '<' . $tag . $attributes . '>';
}
preg_replace_callback( '/<(b|strong)([^>]*)>/si', 'add_extra_strong_class', $post_content );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment