Tengo un formulario de gravedad que toma la entrada de un usuario y luego crea un correo electrónico de notificación a partir de la entrada.
En el backend, tengo la configuración de notificación para crear un correo electrónico html. Genera una etiqueta H2 a partir de las selecciones de una casilla de verificación.
El H2 termina luciendo así: SE BUSCA ASISTENTE DE VETERINARIO: Práctica progresiva, Los mejores clientes, Aprenda en el trabajo, !
Me gustaría eliminar la coma y el signo de exclamación al final de este H2.
Aquí está mi código:
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 );
Sin embargo, cuando envío la notificación, se elimina el signo de exclamación, pero la coma final sigue ahí. ¿Estoy haciendo algo mal con el segundo rtrim()?
El encabezado h2 en la notificación termina luciendo así después de pasar por el filtro:
SE BUSCA ASISTENTE VETERINARIO: Práctica progresiva, Los mejores clientes, Aprenda en el trabajo,
Bueno, la cadena que proporcionaste en este caso tiene un signo de exclamación !
entonces espacio
y luego coma ,
.
Asi que
//trim off the exclamation point $header= rtrim($header, "!"); //now trim off the space $header= rtrim($header, " "); //now trim off the comma at the end $header= rtrim($header, ",");
o en una sola línea:
$header = rtrim($header, "! ,");
Vea el ejemplo de código aquí: http://codepad.org/PNqjFL8h
La habilidad más importante en la depuración es analizar el problema .
La mayor parte del código que nos ha mostrado no es relevante para el problema, y el Ejemplo Mínimo y Reproducible real es este:
$header = 'VETERINARY ASSISTANT WANTED: Progressive Practice, The Best Clients, Learn On the Job, !'; //trim off the exclamation point $new_header = rtrim($header, '!'); //now trim off the comma at the end $new_header_two = rtrim($new_header, ','); echo $new_header_two;
A continuación, mire detenidamente lo que hay en $new_header
, usando una función como var_dump :
string(87) "VETERINARY ASSISTANT WANTED: Progressive Practice, The Best Clients, Learn On the Job, "
Tenga en cuenta que el último carácter no es una coma, es un espacio. Entonces también debes eliminar eso:
$header = 'VETERINARY ASSISTANT WANTED: Progressive Practice, The Best Clients, Learn On the Job, !'; //trim off the exclamation point $new_header = rtrim($header, '!'); // Next there's a space $new_header_two = rtrim($new_header, ' '); //now trim off the comma at the end $new_header_three = rtrim($new_header_two, ','); echo $new_header_three;
Dado que rtrim puede eliminar varios caracteres a la vez, puede simplificar esto en una sola línea:
$header = 'VETERINARY ASSISTANT WANTED: Progressive Practice, The Best Clients, Learn On the Job, !'; // trim off any number of trailing commas, spaces, and exclamation marks $new_header = rtrim($header, '!, '); echo $new_header;
<?php $string = "VETERINARY ASSISTANT WANTED: Progressive Practice, The Best Clients, Learn On the Job, !"; $new_string = rtrim($string, ", !"); echo $new_string; // Outputs: VETERINARY ASSISTANT WANTED: Progressive Practice, The Best Clients, Learn On the Job