Whenever there's necessity to add or strip slashes, consider the pre-defined build-in functions $addslashes & $stripslashes:

https://github.com/atutor/ATutor/blob/master/include/lib/vital_funcs.inc.php#L33-46

They handle different scenarios where PHP setting magic_quotes_gpc is on/off.