urlencode() from PHP to GML
- User
urlencode() from PHP to GML
- Assistant
In GameMaker Language (GML), there is no built-in function equivalent to PHP's
urlencode(). However, you can create a custom script to achieve the same functionality. Here's an example of how you can create aurlencodescript in GML:/// @function urlencode(string) /// @param {string} string - The string to be URL encoded /// @returns {string} The URL encoded string var result = ""; for (var i = 1; i <= string_length(argument0); i++) { var char = string_char_at(argument0, i); var ascii = ord(char); if (ascii >= 65 && ascii <= 90) { // A-Z result += char; } else if (ascii >= 97 && ascii <= 122) { // a-z result += char; } else if (ascii >= 48 && ascii <= 57) { // 0-9 result += char; } else if (ascii == 46 || ascii == 45 || ascii == 95 || ascii == 126) { // . - _ ~ result += char; } else { result += "%" + string_uppercase(string_format("%1$02X", ascii)); // Convert to hexadecimal and append % } } return result;You can use this script as follows:
- User
base64_encode is URL friendly?
- Assistant
No, the
base64_encodefunction in PHP does not create URL-friendly encoded strings by default. The resulting encoded string may contain characters that are not safe for URLs, such as "+" and "/".To create a URL-friendly version of the base64-encoded string, you need to perform some additional steps:
- Use
base64_encodeto encode the string to base64. - Replace the characters "+" with "-", "/" with "_", and remove any trailing "=" characters.
Here's an example of how you can create a
base64UrlEncodefunction in PHP that produces URL-friendly base64-encoded strings:function base64UrlEncode($input) { $base64 = base64_encode($input); $urlFriendly = str_replace(array('+', '/'), array('-', '_'), $base64); $urlFriendly = rtrim($urlFriendly, '='); return $urlFriendly; }You can use this
base64UrlEncodefunction as follows:$originalString = "Hello World!"; $encodedString = base64UrlEncode($originalString); echo $encodedString; // Outputs "SGVsbG8tV29ybGQh"This URL-friendly base64-encoded string can be safely used in URLs without any issues.
- Use