I'd like to suggest a small change in your approach.
It looks like you are processing data to make sure it firts whatever you are going to put the data into.
What I'd suggest is that you don't process the data to make it fit the target,
but change the target so it will clean the data and make it fit itself.
something like:
storeData("hi, I have a trailing space ");
function storeData($psData)
{
$psData = addslashes(trim($psData));
$sQuery = "INSERT INTO table (field) VALUES ('".$psData."')";
}
This way, you never have to worry about cleaning the data, because every time you insert data using this function, it cleans it automatically.