title: How to properly escape a string for use in regular expression in PHP?
body: <p>I am trying to escape a string for use in a regular expression in PHP. So far I tried:</p>
  
  <pre><code>preg_quote(addslashes($string));
  </code></pre>
  
  <p>I thought I need <code>addslashes</code> in order to properly account for any quotes that are in the string. Then <code>preg_quote</code> escapes the regular expression characters. </p>
  
  <p>However, the problem is that quotes are escaped with backslash, e.g. <code>\'</code>. But then <code>preg_quote</code> escapes the backslash with another one, e.g. <code>\\'</code>. So this leaves the quote unescaped once again. Switching the two functions does not work either because that would leave an unescaped backslash which is then interpreted as a special regular expression character. </p>
  
  <p>Is there a function in PHP to accomplish the task? Or how would one do it?</p>