Remove special characters from a String in PHP

This article, will discuss multiple ways to remove special characters from a string in PHP.

Table Of Contents

Background

Sometimes, you might need to clean a string in PHP by removing special characters. This is often necessary for data sanitization, URL slugs, username validation, or other types of text processing.

For example, consider the following string:

$originalString = "This@# is!% a $sample& string*";

The goal is to remove the special characters (@, #, !, %, $, &, *) to get a clean string like:

"This is a sample string"

Let’s examine a couple of solutions to achieve this.

Solution 1: Using preg_replace()

The preg_replace() function is a powerful tool for performing regular expression searches and replacements. We can use it to replace all special characters in a string with an empty string, effectively removing them.

Let’s see the complete example,

<?php
$originalString = "This@# is!% a $sample& string*";

// Define the pattern to match special characters
$pattern = '/[^a-zA-Z0-9s]/';

// Replace special characters with an empty string
$cleanString = preg_replace($pattern, '', $originalString);

echo $cleanString;
?>

Output

This is a sample string

In this code:
– $pattern is a regular expression that matches any character that is not a letter, digit, or whitespace.
– preg_replace($pattern, ”, $originalString) replaces all occurrences of the matched characters with an empty string.
– The result is a string with all special characters removed.

Solution 2: Using str_replace()

If you have a predefined set of special characters you want to remove, str_replace() can be a simpler alternative. This function replaces all occurrences of a search string in a string with a replacement.

Let’s see the complete example,

<?php
$originalString = "This@# is!% a $sample& string*";

// Define an array of special characters to be removed
$specialChars = ['@', '#', '!', '%', '$', '&', '*'];

// Replace special characters with an empty string
$cleanString = str_replace($specialChars, '', $originalString);

echo $cleanString; // Outputs: "This is a sample string"
?>

Output

This is a sample string

In this code:
– $specialChars is an array containing all the special characters you want to remove.
– str_replace($specialChars, ”, $originalString) iterates over the array and replaces each special character with an empty string.
– The result is a string with the specified special characters removed.

Summary

To remove special characters from a string in PHP, you can use preg_replace() with a suitable regular expression for a broad approach or str_replace() for targeting specific characters. Both methods are effective for different scenarios and can be chosen based on the specific requirements of your string manipulation task.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top