-2

How can I remove everything before the first ABC in a string?

So that this: somethingrandomABCotherrandomstuffABCmorerandomstuffABC

turns to this: otherrandomstuffABCmorerandomstuffABC.

Is this possible with php?

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
dggdsdg
  • 27
  • 2
  • 2
  • 3
  • is this a generic case or..? also, what is the logic behind this? any rule to follow? – briosheje Oct 14 '14 at 16:32
  • 1
    Explode the string, remove the first occurrence, glue it back together. – Ohgodwhy Oct 14 '14 at 16:33
  • You can replace (by nothing in your case) a lot of things using [regular expressions](http://php.net/manual/en/function.preg-replace.php). – andy Oct 14 '14 at 16:34
  • 1
    Describe exactly what you are trying to achieve. I would bet you are creating a complex solution to a very simple problem. Why have you got these wierd string, do you create them yourself? – RiggsFolly Oct 14 '14 at 16:41

2 Answers2

10

There is a php built in for doing this: strstr

Combine with substr to strip out your token:

$out = substr(strstr($text, 'ABC'), strlen('ABC'))
Parris Varney
  • 11,320
  • 12
  • 47
  • 76
4
<?php

function removeEverythingBefore($in, $before) {
    $pos = strpos($in, $before);
    return $pos !== FALSE
        ? substr($in, $pos + strlen($before), strlen($in))
        : "";
}

echo(removeEverythingBefore("somethingrandomABCotherrandomstuffABCmorerandomstuffABC", "ABC"));

?>

Outputs:

otherrandomstuffABCmorerandomstuffABC
Kuba Wyrostek
  • 6,163
  • 1
  • 22
  • 40