addcslashes
Thursday, June 22nd, 2006 | Life
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).
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).