0

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

  • under 500 characters
  • end with B%22, so that the next iteration of the string starts with
  • partNumber%22

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?

1
  • Hi, check your string by decoding on meyerweb.com/eric/tools/dencoder . Your string is a JSON string and you can simply decode it then parse it. This would be the best way. Is the API Restful? If so, just pass the pure JSON after the conversion. Commented Apr 3, 2015 at 15:06

1 Answer 1

1

use explode.

$apiStrings = explode("B%22", $string);
foreach($apiStrings as $apiString)
{
    //Do request
}
Sign up to request clarification or add additional context in comments.

4 Comments

I'd thought about that, but this forces me to do ~500 searches, which is incredibly slow. With the length of a general part number, I can do about 50 parts in a single search, so I'd prefer not to break it up into something THAT small.... if possible.
You could explode, check string length, and concat strings together as needed to form a longer request.
This is the wrong way, he should not split the string like this. The string is a json when it is decoded. So something more compatible and uniform should be written to accommodate future requirements.
@JohnWu how exactly is this string generated? Do you generate it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.