Quote:
Originally Posted by jdlovins
Awesome. Something that would be very useful is a URL encode function. For example if you call an API with parameters such as
/items?type=5&name=i have spaces
it will error out on the API end because of the spaces. Not sure how easy that is to solve really.
|
"/items?type=5&name=i+have+spaces"
As this thing seems to be aimed at developers making their own things, I assume you're developing your own stuff that uses this thing, which means you could (remember code attribution!) "copy" this one:
From
Dynamic MOTD by
@psychonic:
UrlEncodeString
PHP Code:
// Taken from Dynamic MOTD by psychonic - https://forums.alliedmods.net/showthread.php?t=147193
// loosely based off of PHP's urlencode
void UrlEncodeString(char[] output, int size, const char[] input)
{
int icnt = 0;
int ocnt = 0;
for(;;)
{
if (ocnt == size)
{
output[ocnt-1] = '\0';
return;
}
int c = input[icnt];
if (c == '\0')
{
output[ocnt] = '\0';
return;
}
// Use '+' instead of '%20'.
// Still follows spec and takes up less of our limited buffer.
if (c == ' ')
{
output[ocnt++] = '+';
}
else if ((c < '0' && c != '-' && c != '.') ||
(c < 'A' && c > '9') ||
(c > 'Z' && c < 'a' && c != '_') ||
(c > 'z' && c != '~'))
{
output[ocnt++] = '%';
Format(output[ocnt], size-strlen(output[ocnt]), "%x", c);
ocnt += 2;
}
else
{
output[ocnt++] = c;
}
icnt++;
}
}
__________________