Right now, I can use preg_replace
to add something at the beginning of an HTML attribute this way:
$output = preg_replace('/class="/', 'class="$new_class, $output, 1);
How can I add something at the end of the HTML attribute?
Right now, I can use preg_replace
to add something at the beginning of an HTML attribute this way:
$output = preg_replace('/class="/', 'class="$new_class, $output, 1);
How can I add something at the end of the HTML attribute?
You should change your regex to:
(class="[^"]*)
which means capturing a class="
and everything else up to a double quotation mark "
. Then use \1
back-reference to first capturing group in replacement string for appending new class to it:
preg_replace('/(class="[^"]*)/', "$1 $new_class", $output);