Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There is a very simple way to do this, based on <a href="http://php.net/preg_replace" rel="nofollow noreferrer"><code>preg_replace</code><sup><em>Doc</em></sup></a> and <a href="http://php.net/stripcslashes" rel="nofollow noreferrer"><code>stripcslashes</code></a>, both build in:</p> <pre><code>preg_replace( '/\\\\([nrtvf\\\\$"]|[0-7]{1,3}|\x[0-9A-Fa-f]{1,2})/e', 'stripcslashes("$0")', $content ); </code></pre> <p>This works as long as <code>"\\n"</code> should become <code>"\n"</code> and the like. <a href="http://codepad.org/FGtxSlX3" rel="nofollow noreferrer">Demo</a>.</p> <p>If you're looking for processing these strings literally, see my <a href="https://stackoverflow.com/a/8311920/367456">previous answer</a>.</p> <p><strong>Edit:</strong> You asked in a comment:</p> <blockquote> <p>I'm just a bit puzzled what's the difference between the output of this and stripcslashes() directly [?]</p> </blockquote> <p>The difference is not always visible, but there is one: <code>stripcslashes</code> will remove the <code>\</code> chracter if no escape sequence follows. In PHP strings, the slash is not be dropped in that case. An example, <code>"\d"</code>, <code>d</code> is not a special character, so PHP preserves the slash:</p> <pre><code>$content = '\d'; $content; # \d stripcslashes($content); # d preg_replace(..., $content); # \d </code></pre> <p>That's why <code>preg_replace</code> is useful here, it will only apply the function on those substrings where <code>stripcslashes</code> works as intended: all valid escape sequences.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload