How to Remove Special Characters From Array in PHP

Last Updated on December 27, 2020 by Roshan Parihar

In this tutorial, learn how to remove special characters from an array in PHP. The short answer is to use the PHP preg_replace() to delete all special characters and PHP str_replace() to remove specified special characters. You can remove single, multiple, and all special characters from an array using PHP.

There are many useful PHP functions whose examples given below to delete special characters from an array.

Let’s find out how you can use these functions to delete those characters from the given array in PHP.

Remove All Special Characters From Array in PHP

To remove all special characters from an array in PHP, you have to use the PHP preg_replace() that removes all the special characters. See the example below to learn the process.

Output

Array ( [0] => I [1] => am [2] => from [3] => India [4] => country )

The above example containing an array with some special characters. The resulted output of the example contains no special characters. It removes all the special characters without any space in place of them.

How to Eliminate Specified Symbols From Array Using PHP

To remove only the specified special characters from an array, use the PHP str_replace() function that replaces only the specified special characters.

You can use str_replace() function of PHP to delete single or multiple characters from an array.

Remove Single Specified Character from Array

If you want to remove the single character from an array, you have to specify the special character in the first argument and the array variable in the second argument.

Output

Array ( [0] => $I [1] => .am [2] => from [3] => @India [4] => +country )

The above example removes the single special character ‘&’ from an array with no space. The above array contains the ‘&’ character and the resulted output contains no special character ‘&’.

How to Delete Multiple Symbols

In addition to the above, you can also delete the multiple symbols from an array. Place all the special characters in the first argument that you want to remove. Also, add an array variable in the second argument from which you have to delete symbols.

Output

Array ( [0] => I [1] => .am [2] => from [3] => @India [4] => country )

The above example deleted multiple symbols from an array. There are 3 specified special symbols to delete using the above PHP example.

You May Also Like to Read

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.