Free cookie consent management tool by TermsFeed Generator PHP Sanitize Data | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP Sanitize Data

PHP Sanitize Data

Last Updated on Feb 15, 2023

Introduction

Sanitizing data is a very important step, especially when you are dealing with user data.

What does it mean to sanitize data?

It means to clean up your data by using deleting, replacing, encoding, or escaping techniques.

Sanitize Data

Do you remember when we used filters to validate email?

We are going to use the same function but with different flags to sanitize data

So let’s see an example

filter_var($string, FILTER_SANITIZE_STRING);

Here it doesn’t validate the data to tell us if it’s string or not. It sanitizes it. It cleans up the string.

So if we have an input like this:

$string = '<p>here is a html paragraph</p>';

It would clean and return this:

$filtered = filter_var($string, FILTER_SANITIZE_STRING);
// here is a html paragraph

As you can see it has removed the html tags for us.

Here is a list of most useful flags to sanitize your data

FILTER_SANITIZE_STRING

Strip tags and HTML-encode double and single quotes, optionally strip or encode special characters.

if you are using PHP < 8.1 this filter is very useful and powerful. However, If you are using PHP > 8.1, this filter is no longer available and it is recommended to use FILTER_SANITIZE_FULL_SPECIAL_CHARS which I will explain in this post OR use htmlspecialcharacters function instead

$string = '<p>here is a html paragraph</p>';
$filtered = filter_var($string, FILTER_SANITIZE_STRING);
// here is a html paragraph

FILTER_SANITIZE_EMAIL

Remove all characters except letters, digits and !#$%&'*+-=?^_`{|}~@.[].

$string = '<example>email@test€.com';
$filtered = filter_var($string, FILTER_SANITIZE_EMAIL);
// exampleemail@test.com

FILTER_SANITIZE_URL

Remove all characters except letters, digits and $-_.+!*'(),{}|\\^~[]`<>#%";/?:@&=.

$string = '<example>url°test€.com';
$filtered = filter_var($string, FILTER_SANITIZE_URL);
// <example>urltest.com

FILTER_SANITIZE_ENCODED

URL-encode string, optionally strip or encode special characters.

$string = 'test=example query & another=test query!';
$filtered = filter_var($string, FILTER_SANITIZE_ENCODED);
// test%3Dexample%20query%20%26%20another%3Dtest%20query%21

FILTER_SANITIZE_ADD_SLASHES

Apply addslashes().

$string = "have you visited amir's website!";
$filtered = filter_var($string, FILTER_SANITIZE_ADD_SLASHES);
// have you visited amir\'s website!

FILTER_SANITIZE_NUMBER_FLOAT

Remove all characters except digits, +- and optionally .,eE. 

You should add the option FILTER_FLAG_ALLOW_FRACTION if you want to keep the decimals.

$string = "$616.15&";
$filtered = filter_var($string, FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
// 616.15

FILTER_SANITIZE_NUMBER_INT

Remove all characters except digits, plus and minus sign.

$string = "$-15&";
$filtered = filter_var($string, FILTER_SANITIZE_NUMBER_INT);
// -15

FILTER_SANITIZE_FULL_SPECIAL_CHARS

Equivalent to calling htmlspecialchars() with ENT_QUOTES set.

$string = "characters like < and &";
$filtered = filter_var($string, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
// characters like &lt; and &amp;

You can see a list of all the filters and flags on php's official website.

Conclusion

Now you know about Sanitizing data in PHP.

I recommend you to open a PHP files and try to Sanitizing different types of data.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • Sanitizing data in php
  • filter var
  • cleaning up the data in php
  • FILTER_SANITIZE_STRING
  • FILTER_SANITIZE_EMAIL
  • FILTER_SANITIZE_URL
  • FILTER_SANITIZE_ENCODED
  • FILTER_SANITIZE_ADD_SLASHES
  • FILTER_SANITIZE_NUMBER_FLOAT
  • FILTER_SANITIZE_NUMBER_INT

Category: programming

Tags: #php

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

the ultimate guide to Git: learn everything you need in less than 1 hour
Nov 28, 2022 programming

the ultimate guide to Git: learn everything you need in less than 1 hour

Here is everything you need to know about git in a series of short question and answers. ...

11 Min Read Read More
Deploy PHP to Heroku
Feb 14, 2023 programming

Deploy PHP to Heroku

Today we are going to talk about deploying your PHP on heroku. Having your code on your local machine is one thing and deploying it on a server and sharing it with the world is another thing. ...

7 Min Read Read More
A Programmer's Guide to Debugging: Essential Steps to Follow
Mar 23, 2024 programming

A Programmer's Guide to Debugging: Essential Steps to Follow

Debugging is a crucial skill for any programmer, as it helps identify and fix issues in the code. Effective debugging not only improves the overall quality of your software but can also save you time and frustration. ...

10 Min Read Read More
Creating a Simple Jupyter Notebook in PHP
Aug 28, 2024 programming

Creating a Simple Jupyter Notebook in PHP

This tutorial will guide you through the steps to create a simple PHP-based notebook interface. The notebook allows you to write and run PHP code in a web browser, maintaining the state between code executions. ...

28 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...