1

I have the following strings:

$string="British Airways BAGvpWdLarbvapDA0xYf9B71KVh1ySPSzGycWyiRtYJJ";

and $del_sub_two="KVh1ySPSzGycWyiRtYJJ"; which I use as a delimiter within strings.

When I run: $string=trim($string,$del_sub_two); the result is:

British Airways BAGvpWdLarbvapDA0xYf9B7

But it should be:

British Airways BAGvpWdLarbvapDA0xYf9B71

I need it to remove the $del_sub_two string from the end of any particular string, why is it removing an extra character at the end of the string?

dlofrodloh
  • 1,728
  • 3
  • 23
  • 44

3 Answers3

2

Alright, reading your comment on the other answers, you want to remove the string KVh1ySPSzGycWyiRtYJJ whenever it's at the end of any other string. Use regex.

$string="British Airways BAGvpWdLarbvapDA0xYf9B71KVh1ySPSzGycWyiRtYJJ";
$string = preg_replace('/KVh1ySPSzGycWyiRtYJJ$/', '', $string);
Daniel
  • 1,229
  • 14
  • 24
  • 1
    @chris85, actually this would be fine for my requirement as the string is always at the end – dlofrodloh Apr 22 '16 at 21:57
  • I thought so @dlofrodloh – Daniel Apr 22 '16 at 21:58
  • 1
    @Daniel, thanks I probably should have been clearer about that in my original question – dlofrodloh Apr 22 '16 at 21:59
  • 1
    If you ever want to change it to remove the _last occurance_ you can take a look at this question: http://stackoverflow.com/questions/3835636/php-replace-last-occurence-of-a-string-in-a-string and see if it would be of any use to you. – Daniel Apr 22 '16 at 21:59
  • The code you put into your answer is the accepted question at the link I posted above..? @Anant – Daniel Apr 22 '16 at 22:05
1

trim() doesn't remove a specific string, it removes any items in the string you specify in the second parameter you pass in from the start and end of the target string. Because you passed in a string with the value 1 in the 2nd parameter, all instances of 1 will be removed from the beginning and end of the string you want to trim.

For example, if you add a capital B to the second parameter of your trim function, it will remove the B from British Airways.

Example:

$string="British Airways BAGvpWdLarbvapDA0xYf9B71KVh1ySPSzGycWyiRtYJJ";
$del_sub_two="KVh1ySPSzGycWyiRtYJJ";

$string=trim($string, 'BKVh1ySPSzGycWyiRtYJJ');

// Echos "ritish Airways BAGvpWdLarbvapDA0xYf9B7"
echo $string;
Lloyd Banks
  • 35,740
  • 58
  • 156
  • 248
  • Thanks, but why is there lower case i's in the result string (e.g. British Airways) when it's included in the parameter string? – dlofrodloh Apr 22 '16 at 21:38
  • because it affects first and last characters only. – Alive to die - Anant Apr 22 '16 at 21:40
  • @dlofrodloh Only the start and end of the string are affected by `trim()` – Lloyd Banks Apr 22 '16 at 21:40
  • But it only knows the beginning and end of the string based on the string you pass as a parameter right? So surely the sequence matters then. How else would it know where to begin? If you look at the output in my question, it removes the entire $del_sub_two string, not just the first or last character... but it takes away an extra character which is the problem – dlofrodloh Apr 22 '16 at 21:42
0

Alternative solution:

$string="British Airways BAGvpWdLarbvapDA0xYf9B71KVh1ySPSzGycWyiRtYJJ";

$del_sub_two="KVh1ySPSzGycWyiRtYJJ";

$pos = strpos($string, $del_sub_two);

if($pos === FALSE) {
   echo "not found";
} else {
  // will output: British Airways BAGvpWdLarbvapDA0xYf9B71
  echo substr($string, 0, $pos);
}
jasonlam604
  • 1,456
  • 2
  • 16
  • 25