1

I have this string:

"(data I don't care about) (invariable substring) (data I care about)"

How do I trim all the data I don't care about, knowing the invariable substring?

Michael Myers
  • 188,989
  • 46
  • 291
  • 292
lemony
  • 11
  • 1
  • Possible duplicate of [PHP remove all characters before specific string](https://stackoverflow.com/questions/7802821/php-remove-all-characters-before-specific-string) – Vladislav Nov 22 '17 at 14:09

3 Answers3

2

This should suffice:

substr($string, (int)strpos("invariablestring", $string));

But will leave the invariablestring untouched. If you want to remove that string, just add it's length to the value strpos() returned.

smottt
  • 3,272
  • 11
  • 37
  • 44
1

Simplest I've found is:

list(, $data_i_care_about) = explode('(invariable substring)', $all_the_data, 2)
Francis
  • 11
  • 1
1

You could get the offset and only take the substring from that offset on:

if (($pos = strpos($str, $substr)) !== false) {
    $str = substr($str, $pos);
}
Gumbo
  • 643,351
  • 109
  • 780
  • 844