DEV Community

Cover image for PHP Sanitizer.
Erland Muchasaj
Erland Muchasaj

Posted on • Originally published at Medium

PHP Sanitizer.

On the new year's eve, I set out to build some open source packages for PHP and Laravel community which is one of the best communities you can find. The packages would consist of libraries, utils, helpers and packages and also some starters for medium and large-scale projects.

I have been working with PHP and related technologies for over a decade now and more often than not, I find myself using the same peace of code in different projects.

Up until now I used to create Libraries which I would just transfer files from one project to another and reuse the library, but I wanted something more manageable and if I wanted to change something or fix issues to easily fix it in one place.

Backstory

In my early days in programming, I often had to create search pages using vanilla PHP. 
This means I needed to get user input, parse it, sanitize it, and prepare for the query. 
Another thing to consider was special characters for different languages for example:

э => e
Ë => e
Ç => c
Enter fullscreen mode Exit fullscreen mode

For this reason I build a PHP Sanitizer class to only do just that. Sanitize user input and prepare it for SQL search queries.

GitHub logo erlandmuchasaj / sanitize

A package to sanitize your input.

PHP sanitize

This is a PHP package that helps to sanitize your input when performing search queries to DB It cleans the input from all malicious characters.

Installation

You can install the package via composer:

composer require erlandmuchasaj/sanitize
Enter fullscreen mode Exit fullscreen mode

Usage

Sanitize library offers a sanitize method that takes a parameter a dirty string parameter and return a cleaned string.

use ErlandMuchasaj\Sanitize\Sanitize;

$dirtyString = 'Hello - World';

$sanitizedString = Sanitize::sanitize($dirtyString);

// do something with the sanitized text
// you can: $words = explode(' ', $sanitizedString);
// and search the DB per each word.
Enter fullscreen mode Exit fullscreen mode

Support me

I invest a lot of time and resources into creating best in class open source packages.

If you found this package helpful you can show support by clicking on the following button below and donating some amount to help me work on…



It is very simple to use. I will do a follow up with some examples of how to use it on your projects.

Let me know in the comment section below if you ever use it in any of your projects.
Don't forget to like and comment.
Follow me for more web development tips, new packages and more.
Thanks for reading.

Top comments (0)