Reputation: 3
I have an array with with data from the database, something like this:
array(4) { [0]=> string(9) "336_20235" [1]=> string(9) "336_20237" [2]=> string(9) "336_20239" [3]=> string(9) "336_20241" }
There are all elements from DB. I need to prepare string from this data, comma separated, but I have a limitation to 1000 signs. So I'm using explode function, and my string looks like this:
336_20235,336_20237,336_20239,336_20241
but when string lenght will be over 1000 signs, I need to split string, so I'm doing this:
(explode('|',(wordwrap(implode(', ', $mystring), 1000, '|', true))))
So now I have for example two arrays with strings, like this:
[0]=> string(999) "3-9782, 3-9781, 3-9776, 3-9775, 3-9783, 3-9780, 3-9779, 3-9778, 3-9777, 1-9756, 4-9789,
As You can see, I have a space after comma. I don't need that space. I know I can do this:
str_replace(' ', '',(explode('|',(wordwrap(implode(', ', $mystring), 1000, '|', true)))));
After that string looks like this:
[0]=> string(887) "3-9782,3-9781,3-9776,3-9775,3-9783,3-9780,3-9779,3-9778,3-9777
So now the string have 887 signs, and it's ok, but It's about API calls, with limitation to 1000 signs and 200 calls in hour. So I could place more data from the array here, and limit the API calls, but wordwrap function needs space to properly slice elements.
I wonder if there is better way to do this?
Upvotes: 0
Views: 266
Reputation: 42715
You can use regular expression matching to limit the size of the strings:
$data = [234234, 35464564, 23436232, 6508482, 234892, 34534524323, 43453, 2412, 34534523];
$limit = 20;
// fill $matches[0] with the length-limited strings
preg_match_all("/.{1,$limit}(?=,)/", implode(",", $data), $matches);
// this can leave leading or trailing commas, trim them off
array_walk($matches[0], function(&$v, $k){$v = trim($v, ",");});
var_dump($matches[0]);
Upvotes: 0
Reputation: 57121
The way I've approached this is to try and split the string by the last comma before the 1000 character (defined in $splitAt
). So what it does is to first add them all together with comma separators. Then look for the last comma in the first 1000 characters using strrpos()
(storing this in $splitPos
). Then extract the first part and store it in the output and carries on with the remaining string till there is nothing else to work with...
$long = implode(",", $source).",";
$splitAt = 1000;
$split = [];
while ( $long ) {
$splitPos = strrpos(substr($long,0, $splitAt), ",");
$split[] = substr($long, 0, $splitPos);
$long = substr($long, $splitPos+1);
}
print_r($split);
Upvotes: 2
Reputation: 830
As i perceived you main problem is because when you are making string it comes with space after every comma, hence it consumes your string sign length.
So you can avoid this by just removing the space after comma in your implode function like:
implode(',', $mystring) instead of `implode(', ', $mystring)`
Hope this works for you. Or let know if you have different problem.
As you mentioned, can you please tell me more where and what method you are using for Slicing as you mentioned "wordwrap function needs space to properly slice elements." As i can see, you are using simply explode function for slicing. please clear.
Upvotes: 0