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
  • 9,233
  • 18
  • 57
  • 79

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
  • 78,200
  • 7
  • 66
  • 87
FoulFoot
  • 655
  • 5
  • 9
1

You can simply use preg_replace to achieve the desired output:

$output = preg_replace('/fghijk$/s', '', 'abcdefghijk');
akshaypjoshi
  • 1,245
  • 1
  • 15
  • 24
1

You can always use preg_replace

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

Test online

ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38