How to Remove any Special Character From PHP Array?

03-Apr-2023

.

Admin

How to Remove any Special Character From PHP Array?

Hi Dev,

Today, how to remove any special character from php array is our main topic. This post will give you simple example of how to remove special character from array in php. I explained simply about can i remove a specific item from an array php. we will help you to give example of remove special character in php.

There are to example to Check if remove any special character from php array. in this example, we will use to preg_replace() and str_replace()function to Check if String Contains Dash. so Let's the following tow example with output.

Remove All Special Characters From Array in PHP


index.php

<?php

$myArray = array('$I', '.am', '&from', '@India','+country');

// Get remove all special characters of array

$newArray = preg_replace('/[^a-zA-Z0-9_ -]/s','',$myArray);

print_r($newArray);

?>

Output:

Array

(

[0] => I

[1] => am

[2] => from

[3] => India

[4] => country

)

Remove Single Specified Character from Array

index.php

<?php

$myArray = array('$I', '.am', '&from', '@India','+country');

// Get remove single specified character of array

$newArray = str_replace('&', '', $myArray);

print_r($newArray);

?>

Output:

Array

(

[0] => $I

[1] => .am

[2] => from

[3] => @India

[4] => +country

)

I hope it could help you...

#PHP