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).

Timeline

Newsletter

Don't have time to check my blog? Get a weekly email with all the new posts. This is my personal blog, so obviously it is 100% spam free.

Metadata

This entry was posted on Thursday, June 22nd, 2006 at 11:50 am and is filed under Life. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.