Sanitizing Variables in PHP without Regular Expressions

sql-injection-1I spend a tough time learning regular expressions but forget it easily within a short span of time. Though cheatsheets come to my rescue but while coding applications I try to stay away from Regular Expressions as much as possible. For those like me, here is the simple way that you can implement to sanitize variables in PHP and avoid SQL Injections.

Sanitizing the single quote (‘) from a variable

function sanitize_vars($content) {
return str_replace("'","",$content);
}

The above function will remove all the single quotes found in the $content variable

Sanitizing the double quotes (“) from a variable

function sanitize_vars($content) {
return str_replace("\"","",$content);
}

The above function will remove all the double quotes found in the $content variable

Sanitizing empty spaces from a variable

function sanitize_vars($content) {
return str_replace(" ","",$content);
}

The above function will remove all the empty spaces found in the $content variable

Sanitizing multiple symbols from a variable

function sanitize_vars($content) {
return str_replace(
array ("'","", " ", "$", "%", "@", "#", "^", "&", "*")
,"",$content);
}

The above function will remove all the following symbols from the $content variable – single quotes, double quotes, spaces, $,%,#,^,&,*

Related posts:

  1. How to Validate email address with PHP
  2. Simple Database Class for PHP
  3. How to Insert content after each post in WordPress
  4. How to Control A PHP Web Application Remotely
  5. How to remind your visitors to stumble you


The author is a small business owner himself specialising in Web Development. He runs Insight Studios which offers specialized Web Development, SEO Services and PPC Management for Small Business and SME thus ensuring growth and prosperity using the power of social media.

Share This Post

Related Articles

Leave a Reply

© 2012 MyGeekPal. All rights reserved. Site Admin · Entries RSS · Comments RSS
Designed by Insight Studios