Home > Mobile >  How to remove characters at the end of a string in PHP using rtrim function properly
How to remove characters at the end of a string in PHP using rtrim function properly

Time:10-08

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,

CodePudding user response:

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

CodePudding user response:

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;

CodePudding user response:

<?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

CodePudding user response:

You can do something like (not using rtrim) :

$str = 'VETERINARY ASSISTANT WANTED: Progressive Practice, The Best Clients, Learn On the Job, !';

$string = implode(',', array_slice(explode(',', $str), 0, -1 ));
//Output => VETERINARY ASSISTANT WANTED: Progressive Practice, The Best Clients, Learn On the Job

CodePudding user response:

rtrim() can take a list of character and metacharacters. The advantage of this is you can build a list of items to be removed from the end of the string instead of doing individual removals.

// Sample string
$str = 'VETERINARY ASSISTANT WANTED: Progressive Practice, The Best Clients, Learn On the Job, !';

// Full list for unwanted characters
$rmv = ",! \n\r\t\v\0";

// test it
var_dump(rtrim($str,$rmv));

Which will output:

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