urlencode
urlencode(string $string): string
Encodes a string per application/x-www-form-urlencoded (spaces become plus signs, other unsafe characters become %XX).
C C23
#include <ctype.h>
#include <stdio.h>
int main(void) {
const char *s = "a b c!";
char out[64];
int j = 0;
for (int i = 0; s[i] != '\0'; i++) {
unsigned char ch = (unsigned char)s[i];
if (isalnum(ch) || ch == '-' || ch == '_' || ch == '.') {
out[j++] = (char)ch;
} else {
j += sprintf(out + j, "%%%02X", ch);
}
}
out[j] = '\0';
printf("%s\n", out);
return 0;
}
a%20b%20c%21
C++ C++26
#include <cctype>
#include <iostream>
#include <string>
int main() {
std::string s = "a b c!";
std::string out;
char buf[4];
for (unsigned char ch : s) {
if (std::isalnum(ch) || ch == '-' || ch == '_' || ch == '.') {
out += (char)ch;
} else {
snprintf(buf, sizeof(buf), "%%%02X", ch);
out += buf;
}
}
std::cout << out << "\n";
return 0;
}
a%20b%20c%21
C# v11.0.100
Console.WriteLine(Uri.EscapeDataString("a b c!"));
a%20b%20c%21
Go v1.27.4
package main
import (
"fmt"
"net/url"
)
func main() {
fmt.Println(url.QueryEscape("a b c!"))
}
a+b+c%21
Java v27.0.1
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
public class Main {
public static void main(String[] args) {
System.out.println(URLEncoder.encode("a b c!", StandardCharsets.UTF_8));
}
}
a+b+c%21
JavaScript v26.13.1
console.log(encodeURIComponent("a b c!"));
a%20b%20c!
PHP v8.6.0
echo urlencode("a b c!");
a+b+c%21
Python v3.15.2
from urllib.parse import quote, quote_plus
s = "a b c!"
print(quote(s), quote_plus(s))
a%20b%20c%21 a+b+c%21
Rust v1.99.1
fn main() {
let s = "a b c!";
let mut out = String::new();
for b in s.bytes() {
if b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'.') {
out.push(b as char);
} else {
out.push_str(&format!("%{:02X}", b));
}
}
println!("{}", out);
}
a%20b%20c%21
TypeScript v26.13.1
const input: string = "a b c!";
console.log(encodeURIComponent(input));
a%20b%20c!
Language differences
C: hand-written with the %20 dialect (keeping alphanumerics and -_., no third-party deps); mind the C gotcha that isalnum is locale-sensitive (cast to unsigned char first).
C++: hand-written with the %20 dialect.
C#: the path dialect (spaces to %20, RFC 3986) — Uri.EscapeDataString.
Go: the form/query dialect (spaces to +, RFC 1738/HTML forms) — url.QueryEscape; it also keeps */_-..
Java: the form dialect — URLEncoder.encode; it also keeps */_-..
JavaScript: the path dialect encodeURIComponent (demonstrated here); the output a%20b%20c! is the only outlier in the table — encodeURIComponent additionally leaves ! ~ * ' ( ) unescaped, wider than RFC 3986 unreserved, so strict encoders must handle those six by hand.
PHP: the plus-versus-%20 split is the watershed between the two URL-encoding dialects — the form/query dialect (spaces to +, RFC 1738/HTML forms): urlencode; the path dialect (spaces to %20, RFC 3986): rawurlencode; the classic mixing bug: a+b decodes to a b under the plus dialect, while %20-dialect decoders leave the plus untouched.
Python: form dialect quote_plus; path dialect quote (default safe='/' keeps slashes).
Rust: hand-written with the %20 dialect.
TypeScript: same as JS — encodeURIComponent.