Brian Powell
Brian Powell

Reputation: 3411

php break string into smaller parts based on string, not length

I have read up on the php functions wordwrap and chunk_split but I can't figure out how to break down a string into smaller chunks when there are no physical breaks in the string.

I have a URL-encoded string:

%5B%7B%22partNumber%22%3A%2243160-1104%22%7D%2C%7B%22partNumber%22%3A%2242410-6170%22%7D%2C%7B%22partNumber%22%3A%2222-10-2021%22%7D%2C%7B%22partNumber%22%3A%2255091-0674%22%7D%2C%7B%22partNumber%22%3A%2243160-0106%22%7D%2C%7B%22partNumber%22%3A%2287832-1420%22%7D%2C%7B%22partNumber%22%3A%2273415-1001%22%7D%2C%7B%22partNumber%22%3A%2253627-1274%22%7D%2C%7B%22partNumber%22%3A%2243650-0510%22%7D%5D

of a bunch of part numbers I'm feeding into an API. This API can only take 500 characters at a time before it returns a false to me, so I need to break my string down to UNDER 500 characters, but still be a complete, searchable string.

Meaning - however it's broken down, each iteration of this new string needs to be

I'm not sure how I would accomplish this using the wordwrap + explode method as I've only ever used this to break a string by length. Is there a function similar to this that I can use where I can specify an exact string to break at after so many characters?

Upvotes: 0

Views: 75

Answers (1)

James Spence
James Spence

Reputation: 2070

use explode.

$apiStrings = explode("B%22", $string);
foreach($apiStrings as $apiString)
{
    //Do request
}

Upvotes: 1

Related Questions