4 Ways To Remove Special Characters From String In PHP

Special characters in strings can sometimes cause issues when working with data in PHP. These characters can include punctuation marks, symbols, whitespace, and non-printable characters. If you need to clean a string by removing special characters, PHP provides various methods and functions to achieve this.

PHP Remove Special Characters From String

Table of Contents #
  1. preg_replace() with Regular Expressions
  2. filter_var() with FILTER_SANITIZE_STRING
  3. Remove Specific Characters
  4. Remove Extra Whitespace

1. preg_replace() with Regular Expressions

One of the most versatile ways to remove special characters from a string is by using the preg_replace() function with regular expressions. Regular expressions allow you to define a pattern of characters to match and replace.

Here’s an example of how to remove special characters using preg_replace():

<?php
$inputString = "Hello, @World!";

// Remove special characters using a regular expression
$cleanString = preg_replace('/[^A-Za-z0-9\-]/', '', $inputString);
$preserveWhitespace = preg_replace('/[^A-Za-z0-9\- ]/', '', $inputString);

echo $cleanString; // Output: HelloWorld
echo $preserveWhitespace; // Output: Hello World

In this example, the regular expression /[^A-Za-z0-9-]/ matches any character that is not an uppercase letter (A-Z), lowercase letter (a-z), digit (0-9), or hyphen (-). The preg_replace() function replaces all such characters with an empty string, effectively removing them.

2. filter_var() with FILTER_SANITIZE_STRING

The filter_var() function in PHP can be used with the FILTER_SANITIZE_STRING filter to remove special characters from a string. This filter removes or encodes special characters and HTML entities from a string.

Here’s an example of how to use filter_var() to remove special characters:

<?php
$inputString = "Hello, <World!>";

// Remove special characters using FILTER_SANITIZE_STRING
$cleanString = filter_var($inputString, FILTER_SANITIZE_STRING);

echo $cleanString; // Output: Hello, World!

In this example, FILTER_SANITIZE_STRING cleans the input string by removing any HTML tags and special characters, leaving only the text content.

3. str_replace() to Remove Specific Characters

If you want to remove specific special characters from a string, you can use the str_replace() function. This function allows you to specify the characters you want to replace and what to replace them with. Here’s an example:

<?php
$inputString = "Hello, @World!";

// Define an array of characters to remove
$specialChars = array("@", "!");

// Remove specific special characters using str_replace()
$cleanString = str_replace($specialChars, '', $inputString);

echo $cleanString; // Output: Hello, World

In this example, we defined an array of special characters to remove, and str_replace() replaced each of them with an empty string.

4. preg_replace() to Remove Extra Whitespace

To remove whitespace characters (such as spaces, tabs, and line breaks) from a string, you can use preg_replace() with a regular expression that matches whitespace characters. Here’s an example:

<?php
$inputString = "This is a string with     extra     spaces.";

// Remove whitespace using a regular expression
$cleanString = preg_replace('/\s+/', ' ', $inputString);

echo $cleanString;
This is a string with extra spaces.

In this example, the regular expression /\s+/ matches one or more whitespace characters (including spaces, tabs, and line breaks) and replaces them with a single space.