Search code examples
phpstringapicallbacktrim

Trim a string until specific character appears


I want to trim a string and delete everything before a specific character, because I am using an API that gives me some unwanted data in its callback which I want to delete.

The Callback looks like this:

{"someVar":true,"anotherVar":false,"items":[  {"id":123456, [...] }

And I only want the code after the [ , so how can I split a string like this? Thank you!


Solution

  • It is JSON, so you could just decode it:

    $data = json_decode($string);
    

    If you really want to trim up to a certain character then you can just find the character's position and then cut off everything before it:

    if (($i = strpos($string, '[')) !== false) {
        $string = substr($string, $i + 1);
    }