lemony
lemony

Reputation: 11

Trim all characters before a certain substring (php)

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?

Upvotes: 1

Views: 990

Answers (3)

Francis
Francis

Reputation: 11

Simplest I've found is:

list(, $data_i_care_about) = explode('(invariable substring)', $all_the_data, 2)

Upvotes: 1

Gumbo
Gumbo

Reputation: 655239

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

if (($pos = strpos($str, $substr)) !== false) {
    $str = substr($str, $pos);
}

Upvotes: 1

smottt
smottt

Reputation: 3330

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.

Upvotes: 2

Related Questions