addcslashes

If you ever find yourself needing to escape literal characters such as \n in PHP, the function you are looking for (and which I have been looking for, for the past few days (on and off at least)) is addcslashes.

This seems to take care of it…

< ?php
$escaped = addcslashes($not_escaped, "\0..\37!@\@\177..\377");
?>

Then use stripcslashes when decoding it. If you are putting it into a database you will need to escape it again when inserting it but you don’t want to escape your literal characters then so you can just use addslashes (or something more secure, try mysql_real_escape_string).

Metadata

This entry was posted on Thursday, June 22nd, 2006 at 11:50 am and is filed under General. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

One Response to “addcslashes”

  1. Kieran O'Shea Says:

    An often over-looked function in php indeed. Full docs for the function and others orientated around it can be found here: http://www.php.net/addcslashes

Leave a Reply