I have a gravity form that takes input from a user and then creates a notification email from the input.
On the backend, I have the notification setup to build out an html email. It generates an H2 tag from selections from a checkbox.
The H2 ends up looking like this: VETERINARY ASSISTANT WANTED: Progressive Practice, The Best Clients, Learn On the Job, !
I would like to remove the comma and the exclamation point at the end of this H2.
Here is my code:
function alter_ad( $notification, $form, $entry ) {
//grab the message portion of the notification
$data = $notification['message'];
//find the h2 header in the notification
preg_match('/<h2>(.*?)<\/h2>/s', $data, $match);
//store the content h2 in variable
$header = $match[1];
//trim off the exclamation point
$new_header = rtrim($header, '!');
//now trim off the comma at the end
$new_header_two = rtrim($new_header, ',');
//now header should have the comma and exclamation at the end removed
//so now I need to find the h2 in the message again and replace its contents with the new
header text
$start = '<h2>';
$end = '</h2>';
$result = replace_content_inside_delimiters($start, $end, $new_header_two, $data);
$notification['message'] = $result;
return $notification;
}
function replace_content_inside_delimiters($start, $end, $new, $source) {
return preg_replace('#('.preg_quote($start).')(.*?)('.preg_quote($end).')#si',
'$1'.$new.'$3', $source);
}
add_filter( 'gform_notification_42', 'alter_ad', 10, 3 );
However, when I submit the notification, it removes the exclamation point, but the trailing comma is still there. Am I doing something wrong with the second rtrim()?
The h2 header in the notification ends up looking like this after running through the filter:
VETERINARY ASSISTANT WANTED: Progressive Practice, The Best Clients, Learn On the Job,