0

Sorry I don't have any code to post to be discussed, it's one simple question

How to rtrim a string beginning from a set of characters?

String: abcdefghijk

What to remove: fghijk

Anything at the end of the string that starts (the part to be removed) with fg

So, if we have dpgjsufgpeiz, remove fgpeiz

Thanks!

medk
  • 8,713
  • 18
  • 54
  • 77

3 Answers3

3

If TRUE, strstr() returns the part of the haystack before the first occurrence of the needle (excluding the needle).

$whatwewant = strstr($input, 'fg', true);
AbraCadaver
  • 77,023
  • 7
  • 60
  • 83
FoulFoot
  • 645
  • 5
  • 9
1

You can simply use preg_replace to achieve the desired output:

$output = preg_replace('/fghijk$/s', '', 'abcdefghijk');
akshaypjoshi
  • 1,218
  • 1
  • 13
  • 23
1

You can always use preg_replace

$str = 'dpgjsufgpeiz'
$str = preg_replace('/fg\w*$/', '', $str);

Test online

ArtisticPhoenix
  • 20,856
  • 2
  • 21
  • 35