How to remove special characters from a String in PHP
  John Mwaniki /   18 Dec 2021

How to remove special characters from a String in PHP

Having a string of text, and a task to remove all or some of special characters from it in PHP, this article explains the different methods of achieving that with demonstrations of multiple examples.

To begin with, what do we mean by special characters?

A special character can be defined as any character that is not considered alphanumeric (an alphabet or a number), ie. if it does not fall within the range 0 - 9 or A - Z. Punctuation marks, symbols, and accent marks are considered special characters.

Stuff on your keyboard such as @, /, {, ), $, &, #, etc are all special characters.

In PHP, you can remove some or all of the special characters within the strings using:

  • The str_replace() function
  • The preg_replace() function

Method 1: Using str_replace() function

The str_replace() is an inbuilt function in PHP that is used to replace some characters with some other characters in a string.

Syntax

str_replace(find,replace,string,count)

Parameters

  • The find is a mandatory parameter that specifies the value (character) we want to find and replace.
  • The replace is a mandatory parameter that specifies the value we should replace the character with. In this case, we will use an empty string ("").
  • The replace is a mandatory parameter that specifies the string in which we want to remove the various special characters.
  • The count is an optional parameter that is a variable that counts the number of replacements. In this case, we won't really need it.

The function returns a string with the specified special characters removed.

Example

<?php
$str = "Hey there! How are you doing?";
echo str_replace("?", "", $str);

Output:

Hey there! How are you doing

You can see that "?", which we have specified in our string has been removed.

To remove more than one specific character from a string, you will need to specify them all in an array as the first parameter of the function.

Example

<?php
$str = "Hey @there#! How ( are) you *doing/?";
$remove = array("@", "#", "(", ")", "*", "/");
echo str_replace($remove, "", $str);

Output:

Hey there! How are you doing?

As you can see, we have removed all the specified characters ("@", "#", "(", ")", "*", "/") from the string.

This method is best used to remove specific characters but is not suitable for removing all special characters.

The str_ireplace() function is equivalent to str_replace() in all aspects with their only difference being case sensitivity. str_replace() is case-sensititve while str_ireplace() is case-insensitive.

Method 2: Using preg_replace() function

The preg_replace() function replaces all matches of a pattern or list of patterns within a string with substrings.

Syntax

preg_replace(pattern, replacement, input, limit, count)

Parameters

  • The pattern is a mandatory parameter that consists of a regular expression or an array of regular expressions.
  • The replacement is a mandatory parameter that consists of a replacement string or an array of replacement strings.
  • The input is a mandatory parameter that specifies the string or array of strings in which replacements are to be done.
  • The limit is a mandatory parameter that sets a limit on how many replacements should be done in each string.
  • The count is an optional parameter, a variable that contains the number of replacements that have happened after the function has been executed.

The pattern comprises the word or phrase to be replaced enclosed within an opening and closing forward slash /pattern/.

Below is the regular expression (pattern) for alphanumeric characters.

[a-zA-Z0-9] which can also be written as [[:alnum:]]

It matches all alphanumeric values, ie, all values that fall within the range of 0 - 9 and A - Z (upper and lowercase).

If we want to remove all the alphanumeric values from a string, we pass [a-zA-Z0-9] as the pattern in the preg_replace() function and replace it with an empty string "".

<?php
$str = "Hey @there#! How ( are) you *d_oing/?";
echo preg_replace("/[a-zA-Z0-9]/", "", $str);

Output:

@#! ( ) *_/?

Now to remove the special characters from the string, we have to use a regular expression that matches any non-alphanumeric character as shown below.

[^a-zA-Z0-9] which can also be written as [^[:alnum:]]

It's just a negation by adding "^" immediately after the angle bracket. It matches everything that is not part of the pattern. In this case, it matches everything that does not fall within the range of 0 - 9 and A - Z (upper and lowercase).

Example

<?php
$str = "Hey @there#! How ( are) you *d_oing/?";
echo preg_replace("/[^a-zA-Z0-9]/", "", $str);

Output:

HeythereHowareyoudoing

Now as you can see, we have removed all the special characters from the string.

However, as you already have noticed, it has as well removed the spaces and punctuation marks such as full stop, question mark, exclamation marks, etc.

If you want to retain other characters in the string, you need to specify them within the regular expression.

For example, the regular expression below matches lowercase alphabets a-z, uppercase alphabets A-Z, numbers 0-9, whitespaces \s, exclamation mark !, question mark ?, full stop ., comma ,, single quote \' and double quote ".

[a-zA-Z0-9\s!?.,\'\"]

To remove all the characters that do not fall under the above criteria, you have to add ^ after the opening angle bracket so the regular expression to be [^a-zA-Z0-9\s!?.,\'\"].

Example

<?php
$str = "Hey @there#! How ( are) you *d_oing/?";
echo preg_replace("/[^a-zA-Z0-9\s!?.,\'\"]/", "", $str);

Output:

Hey there! How are you doing?

That's it!

You can now comfortably remove specific or all special characters from a string in PHP using the str_replace() and preg_replace() functions.

It's my hope that you found this article useful.