quotemeta
quotemeta(string $string): string
Escapes the string-domain metacharacters: backslash-prefixes each of the POSIX set . \ + * ? [ ^ ] ( $ - unlike preg_quote, which escapes the regex domain; an empty string returns "" (not false) since 8.0.
C C23
/* No quotemeta(3) in libc: hand-write the POSIX metacharacter set
". \\ + * ? [ ^ ] ( $". Zero-initialized output buffers. */
#include <stdio.h>
#include <string.h>
static const char *META = ".\\+*?[]^$()";
static void quotemeta(const char *in, char *out) { /* POSIX set, PHP-compatible */
size_t j = 0;
for (size_t i = 0; in[i]; ++i) {
if (strchr(META, in[i])) out[j++] = '\\';
out[j++] = in[i];
}
out[j] = '\0';
}
int main(void) {
char out[64] = {0}; /* zero-initialized buffer */
quotemeta("1 + 1 = 2 (yes?)", out);
printf("%s\n", out);
quotemeta("file.txt", out);
printf("%s\n", out);
quotemeta("a$b[c]", out);
printf("%s\n", out);
quotemeta("plain", out);
printf("%s\n", out);
return 0;
}
1 \+ 1 = 2 \(yes\?\) file\.txt a\$b\[c\] plain
C++ C++26
// Hand-written POSIX-set escaping over std::string (no std::quotemeta).
#include <iostream>
#include <string>
static std::string quotemeta(const std::string& s) { // POSIX set, PHP-compatible
const std::string meta = ".\\+*?[]^$()";
std::string out;
for (char c : s) {
if (meta.find(c) != std::string::npos) out += '\\';
out += c;
}
return out;
}
int main() {
std::cout << quotemeta("1 + 1 = 2 (yes?)") << "\n";
std::cout << quotemeta("file.txt") << "\n";
std::cout << quotemeta("a$b[c]") << "\n";
std::cout << quotemeta("plain") << "\n";
std::cout << quotemeta("").size() << "\n"; // empty stays empty
}
1 \+ 1 = 2 \(yes\?\) file\.txt a\$b\[c\] plain 0
C# v11.0.100
// Regex.Escape escapes the REGEX domain (a superset with regex semantics);
// PHP's quotemeta is the fixed POSIX set - hand-write it for parity.
using System.Text.RegularExpressions;
static string Quotemeta(string s) { // POSIX set, PHP-compatible
var b = new System.Text.StringBuilder();
foreach (char c in s) {
if (".\\+*?[]^$()".Contains(c)) b.Append('\\');
b.Append(c);
}
return b.ToString();
}
Console.WriteLine(Regex.Escape("1 + 1 = 2 (yes?)")); // regex domain
Console.WriteLine(Quotemeta("1 + 1 = 2 (yes?)")); // POSIX domain
Console.WriteLine(Quotemeta("file.txt"));
Console.WriteLine(Quotemeta("plain"));
1\ \+\ 1\ =\ 2\ \(yes\?\) 1 \+ 1 = 2 \(yes\?\) file\.txt plain
Go v1.27.4
// No strings quotemeta: hand-write the POSIX metacharacter set.
package main
import (
"fmt"
"strings"
)
func quotemeta(s string) string { // POSIX set, PHP-compatible
const meta = ".\\+*?[]^$()"
var b strings.Builder
for _, c := range s {
if strings.ContainsRune(meta, c) {
b.WriteByte('\\')
}
b.WriteRune(c)
}
return b.String()
}
func main() {
fmt.Println(quotemeta("1 + 1 = 2 (yes?)"))
fmt.Println(quotemeta("file.txt"))
fmt.Println(quotemeta("a$b[c]"))
fmt.Println(quotemeta("plain"))
}
1 \+ 1 = 2 \(yes\?\) file\.txt a\$b\[c\] plain
Java v27.0.1
// Pattern.quote wraps the WHOLE string in \Q..\E (a quoting domain, not
// per-char); PHP's quotemeta backslashes a fixed POSIX set - hand-write it.
public class Main {
static String quotemeta(String s) { // POSIX set, PHP-compatible
StringBuilder b = new StringBuilder();
for (char c : s.toCharArray()) {
if (".\\+*?[]^$()".indexOf(c) >= 0) b.append('\\');
b.append(c);
}
return b.toString();
}
public static void main(String[] args) {
System.out.println(java.util.regex.Pattern.quote("a|b")); // \Qa|b\E
System.out.println(quotemeta("1 + 1 = 2 (yes?)"));
System.out.println(quotemeta("file.txt"));
System.out.println(quotemeta("plain"));
}
}
\Qa|b\E 1 \+ 1 = 2 \(yes\?\) file\.txt plain
JavaScript v26.13.1
// No built-in quotemeta; hand-write the POSIX metacharacter set.
function quotemeta(s) { // POSIX set, PHP-compatible
const meta = ".\\+*?[]^$()";
let out = "";
for (const c of s) {
if (meta.includes(c)) out += "\\";
out += c;
}
return out;
}
console.log(quotemeta("1 + 1 = 2 (yes?)"));
console.log(quotemeta("file.txt"));
console.log(quotemeta("a$b[c]"));
console.log(quotemeta("plain"));
1 \+ 1 = 2 \(yes\?\) file\.txt a\$b\[c\] plain
PHP v8.6.0
// quotemeta - backslash-prefixes the POSIX metacharacters: . \ + * ? [ ^ ] ( $
// Domain note: preg_quote escapes the REGEX domain; quotemeta is this fixed
// POSIX set. Empty input returns "" (no longer false) since PHP 8.0.
var_dump(quotemeta("1 + 1 = 2 (yes?)"));
var_dump(quotemeta("file.txt"));
var_dump(quotemeta('a$b[c]')); // single quotes: $ must not interpolate
var_dump(quotemeta("plain"));
var_dump(quotemeta(""));
string(20) "1 \+ 1 = 2 \(yes\?\)" string(9) "file\.txt" string(9) "a\$b\[c\]" string(5) "plain" string(0) ""
Python v3.15.2
# re.escape escapes the REGEX domain (3.7+: only special chars); PHP's
# quotemeta is the fixed POSIX set - hand-write it for parity.
import re
def quotemeta(s): # POSIX set, PHP-compatible
return "".join("\\" + c if c in ".\\+*?[]^$()" else c for c in s)
print(re.escape("1 + 1 = 2 (yes?)")) # regex domain
print(quotemeta("1 + 1 = 2 (yes?)")) # POSIX domain
print(quotemeta("file.txt"))
print(quotemeta("plain"))
1\ \+\ 1\ =\ 2\ \(yes\?\) 1 \+ 1 = 2 \(yes\?\) file\.txt plain
Rust v1.99.1
// No std quotemeta: hand-write the POSIX metacharacter set (the regex crate
// has regex::escape for the regex domain - kept out to stay on std).
fn quotemeta(s: &str) -> String { // POSIX set, PHP-compatible
let meta: Vec<char> = ".\\+*?[]^$()".chars().collect();
let mut out = String::new();
for c in s.chars() {
if meta.contains(&c) {
out.push('\\');
}
out.push(c);
}
out
}
fn main() {
println!("{}", quotemeta("1 + 1 = 2 (yes?)"));
println!("{}", quotemeta("file.txt"));
println!("{}", quotemeta("a$b[c]"));
println!("{}", quotemeta("plain"));
}
1 \+ 1 = 2 \(yes\?\) file\.txt a\$b\[c\] plain
TypeScript v26.13.1
// Same hand-written POSIX set as JavaScript.
function quotemeta(s: string): string {
const meta: string = ".\\+*?[]^$()";
let out: string = "";
for (const c of s) {
if (meta.includes(c)) out += "\\";
out += c;
}
return out;
}
console.log(quotemeta("1 + 1 = 2 (yes?)"));
console.log(quotemeta("file.txt"));
console.log(quotemeta("a$b[c]"));
console.log(quotemeta("plain"));
1 \+ 1 = 2 \(yes\?\) file\.txt a\$b\[c\] plain
Language differences
C/C++/Go/JavaScript/TypeScript/Rust: libc and the standard libraries ship no quotemeta - hand-write the fixed set (exactly the POSIX ten-char set shown, byte-aligned with PHP output). C#: Regex.Escape belongs to the regex domain - its set is a superset (even spaces become \ ) with different semantics; the entry shows both domain outputs side by side. Java: Pattern.quote wraps the whole string in \Q...\E (a quoting domain), likewise different from per-char POSIX. Python: re.escape (3.7+: special chars only) is also the regex domain. PHP: quotemeta escapes the fixed POSIX set . \ + * ? [ ^ ] ( $ (ten chars); preg_quote (see the preg_quote entry) escapes regex-domain chars and takes an optional delimiter argument - the core teaching difference between the two entries: quotemeta is the string domain (POSIX character-class semantics), preg_quote is the PCRE domain; feeding quotemeta output to PCRE is not always safe (it is defined for the POSIX domain), and conversely preg_quote escapes #/- etc. that quotemeta does not; an empty string returns "" since 8.0; for inputs like a$b[c] write them single-quoted (double quotes would interpolate $b away). Teaching anchor: the same verb "escape" is two functions across two domains - mismatched-domain escaping is a silent-bug source.