Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9255059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:37:38+00:00 2026-06-18T11:37:38+00:00

I was looking for a solid article on when double escaping is necessary and

  • 0

I was looking for a solid article on when double escaping is necessary and when it is not, but I was not able to find anything. Perhaps I didn’t look hard enough, because I’m sure there is an explanation out there somewhere, but lets just make it easy to find for the next guy that has this question!

Take for example the following regex patterns:

/\n/
/domain\.com/
/myfeet \$ your feet/

Nothing ground breaking right? OK, lets use those examples within the context of PHP’s preg_match function:

$foo = preg_match("/\n/", $bar);
$foo = preg_match("/domain\.com/", $bar);
$foo = preg_match("/myfeet \$ your feet/", $bar);

To my understanding, a backslash in the context of a quoted string value escapes the following character, and the expression is being given via a quoted string value.

Would the previous be like doing the folloing, and wouldnt this cause an error?:

$foo = preg_match("/n/", $bar);
$foo = preg_match("/domain.com/", $bar);
$foo = preg_match("/myfeet $ your feet/", $bar);

Which is not what I want right? those expressions are not the same as above.

Would I not have to write them double escaped like this?

$foo = preg_match("/\\n/", $bar);
$foo = preg_match("/domain\\.com/", $bar);
$foo = preg_match("/myfeet \\$ your feet/", $bar);

So that when PHP processes the string it escapes the backslash to a backslash which is then left in when its passed to the PCRE interpreter?

Or does PHP just magically know that I want to pass that backslash to the PCRE interpreter… i mean how does it know I’m not trying to \" escape a quote that I want to use in my expression? or are only double slashes required when using an escaped quote? And for that matter, would you need to TRIPLE escape a quote? \\\" You know, so that the quote is escaped and a double is left over?

Whats the rule of thumb with this?

I just did a test with PHP:

$bar = "asdfasdf a\"ONE\"sfda dsf adsf me & mine adsf asdf asfd ";

echo preg_match("/me \$ mine/", $bar);
echo "<br /><br />";
echo preg_match("/me \\$ mine/", $bar);
echo "<br /><br />";
echo preg_match("/a\"ONE\"/", $bar);
echo "<br /><br />";
echo preg_match("/a\\\"ONE\\\"/", $bar);
echo "<br /><br />";

Output:

0

1

1

1

So, it looks like somehow it doesnt really matter for quotes, but for the dollar sign, a double escape is required as I thought.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:37:39+00:00Added an answer on June 18, 2026 at 11:37 am

    Double quoted strings

    When it comes to escaping inside double quotes, the rule is that PHP will inspect the character(s) immediately following the backslash.

    If the neighboring character is in the set ntrvef\$" or if a numeric value follows it (rules can be found here) it gets evaluated as the corresponding control character or ordinal (hexadecimal or octal) representation, respectively.

    It’s important to note that if an invalid escape sequence is given, the expression is not evaluated and both the backslash and character remain. This is different from some other languages where an invalid escape sequence would cause an error instead.

    E.g. "domain\.com" will be left as is.

    Note that variables get expanded inside double quotes as well, e.g. "$var" needs to be escaped as "\$var".

    Single quotes strings

    Since PHP 5.1.1, any backslash inside single quoted strings (and followed by at least one character) will get printed as is and no variables get substituted either. This is by far the most convenient feature of single quoted strings.

    Regular expressions

    For escaping regular expressions, it’s best to leave escaping to preg_quote():

    $foo = preg_match('/' . preg_quote('mine & yours', '/') . '/', $bar);
    

    This way you don’t have to worry about which characters need to be escaped, so it works well for user input.

    See also: preg_quote

    Update

    You added this test:

    "/me \$ mine/"
    

    This gets evaluated as "/me $ mine/"; but in PCRE the $ has a special meaning (it’s an end-of-subject anchor).

    "/me \\$ mine/"
    

    This is evaluated as "/me \$ mine/" and so the backslashes is escaped for PHP itself whereas the $ is escaped for PCRE. This only works by accident btw.

    $var = 'something';
    
    "/me \\$var mine/"
    

    This gets evaluated as "/me \something", so you need to escape the $ again.

    "/me \\\$var mine/"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was looking into this issue but I couldn't find any solid answers for
I have spent 3 days looking this up and I cannot find a solid
Looking for a perl one-liner what will find all words with the next pattern:
I'm looking for a solid open-source commenting engine written in PHP. It needs to
Possible Duplicate: Credit card payment gateway in PHP? I am looking for a solid
I am looking for a solid implementation to allow me to progressively work through
So I have been looking for a solid solution for a sticky footer for
I'm looking for a good, solid reference for proper RDoc syntax. Recommendations? I can't
I have a solid background color on a button, and I'm looking for some
I am looking for a solid tutorial on using the Entity Framework with POCO

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.