Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

245
Visualizações
How to remove characters at the end of a string in PHP using rtrim function properly

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,

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Well, the string you provided in this case has exclamation mark ! then space and then comma ,.

So

 //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, ",");

or in a single line:

$header = rtrim($header, "! ,");

See the code example here: http://codepad.org/PNqjFL8h

over 4 years ago · Santiago Trujillo Relatório

0

The most important skill in debugging is breaking the problem down.

Most of the code you've shown us is not relevant to the problem, and the actual Minimal, Reproducible Example is this:

$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;

Next, look carefully at what is in $new_header, using a function like var_dump:

string(87) "VETERINARY ASSISTANT WANTED: Progressive Practice, The Best Clients, Learn On the Job, "

Notice that the last character is not a comma, it is a space. So you need to remove that as well:

$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;

Since rtrim can strip multiple characters at once, you can simplify this to one line:

$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;
over 4 years ago · Santiago Trujillo Relatório

0

<?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
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda