Last active
December 14, 2015 09:08
-
-
Save josephj/5062476 to your computer and use it in GitHub Desktop.
Splits your combo URL if its length exceeds the max length limitation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function splitUrl(base, files, separator, maxLength) { | |
base = base || "/"; | |
files = files || []; | |
separator = separator || ","; | |
maxLength = maxLength || 1024; | |
var items = [], | |
results = [], | |
url = base + files.join(separator), | |
file, | |
extra = [], | |
i, j; | |
// Checks if url exceeds max length. | |
if (url.length <= maxLength) { | |
results.push(url); | |
return results; | |
} | |
// Adds files one by one to check if it exceeds max length. | |
for (i = 0, j = files.length; i < j; i += 1) { | |
items.push(files[i]); | |
url = base + items.join(separator); | |
// Oops! It exceeds max length!! | |
if (url.length > maxLength) { | |
// Remove the last file from `items` array. | |
file = items.pop(); | |
// Now we can make sure current url doesn't exceed. | |
// Adds it to $results array. | |
results.push(base + items.join(separator)); | |
// Creates new items array for next iteration. | |
items = []; | |
if (file) { | |
items.push(file); | |
} | |
} | |
} | |
// Adds the rest files to `result` array. | |
if (items.length) { | |
url = base + items.join(separator); | |
results.push(url); | |
} | |
return results; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function split_combo_url($base, $files = array(), $separator = ",", $max_length = 1024) | |
{ | |
$results = array(); | |
$url = $base . implode($separator, $files); | |
// Checks if url exceeds max length. | |
if (strlen($url) <= $max_length) | |
{ | |
$results[] = $url; | |
return $results; | |
} | |
// Adds files one by one to check if it exceeds max length. | |
$extra = array(); | |
$len = count($files); | |
for ($i = 0; $i < $len; $i++) | |
{ | |
$items[] = $files[$i]; | |
$url = $base . implode($separator, $items); | |
// Oops! It exceeds max length!! | |
if (strlen($url) > $max_url_length) | |
{ | |
// Remove the last file from $items array. | |
$file = array_pop($items); | |
// We can make sure current url doesn't exceed. | |
// Adds it to $results array. | |
$results[] = $base . implode($separator, $items); | |
// Creates the $items array for next iteration. | |
$items = []; | |
if ($file) | |
{ | |
$items[] = $file; | |
} | |
} | |
} | |
// Adds the rest files to $result array. | |
if (count($items)) | |
{ | |
$url = $base . implode($separator, $items); | |
$results[] = $url; | |
} | |
return $results; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment