200-710 · Question #201
You need to escape special characters to use user input inside a regular expression. Which functions would you use? (Choose 2)
The correct answer is C. preg_quote() E. quote_meta(). Both preg_quote() (C) and quote_meta() (E) are PHP functions specifically designed to escape regex metacharacters (like ., `, ?, (, etc.) so that user input is treated as a literal string within a pattern. preg_quote() is the modern, preferred choice for PCRE patterns (used…
Question
Options
- Aaddslashes()
- Bhtmlentities()
- Cpreg_quote()
- Dregex_quote()
- Equote_meta()
How the community answered
(29 responses)- A14% (4)
- B3% (1)
- C79% (23)
- D3% (1)
Explanation
Both preg_quote() (C) and quote_meta() (E) are PHP functions specifically designed to escape regex metacharacters (like ., *, ?, (, etc.) so that user input is treated as a literal string within a pattern. preg_quote() is the modern, preferred choice for PCRE patterns (used with preg_match, preg_replace, etc.), while quote_meta() escapes a similar set of metacharacters and has been available since PHP 4.
The distractors fail because addslashes() (A) escapes quotes and backslashes for contexts like database queries-not regex metacharacters. htmlentities() (B) converts characters to HTML entities for HTML output safety, which is completely unrelated to regex. regex_quote() (D) simply does not exist in PHP-it's a fabricated function name designed to trick you.
Memory tip: Match the prefix to the purpose-preg_quote() pairs with the preg_* family of functions, and quote_meta() sounds exactly like what it does: "quote the meta[characters]." If a function name doesn't exist in your mental PHP reference (like regex_quote), it's almost certainly a trap.
Topics
Community Discussion
No community discussion yet for this question.