Removing Special Characters in PHP
Dealing with strings has always been one of the most complicated things in all programming languages. In C it is even more daunting, strings do not even exist, talk about array of characters :[
Moving to PHP from a C background, it seemed more easy to deal with strings, there’s almost a function for anything, even for this. The only problem is knowing that the function exists and understanding how to use it, at least that was my problem.
When storing text that contains special characters such as: double quotes, singles quotes, newline character and others, to some databases, escape characters are added to the special characters in order to prevent SQL injection.
When reading this text from the database, the escape characters are not removed automatically but there are some PHP functions that can help do that easily.
stripslashes($string_to_strip)
This function strips off all backslashes from the string passed and returns a string. It’s easy, just pass in the string as a parameter to the function and it’ll work the magic for you :)
stripcslashes($string_to_strip)
This is used in a similar way as the previous function. Unlike the former, this function doesn’t just strip of the backslashes but it converts them to their C equivalent.
To find out more on how to use these two functions, the PHP documentation can be visited.