How to remove any special character from php array?
I have array like:
JavaScript
x
$temp = array (".com",".in",".au",".cz");
I want result as:
JavaScript
$temp = array ("com","in","au","cz");
I got result by this way:
JavaScript
$temp = explode(",",str_replace(".","",implode(",",$temp)));
But is there any php array function with we can directly remove any character from all values of array? I tried and found only spaces can be removed with trim()
but not for any character.
Advertisement
Answer
Use preg_replace function. This will replace anything that isn’t a letter, number or space.
JavaScript
<?php
$temp = array (".com",".in",".aus",".cz");
$temp = preg_replace("/[^a-zA-Z 0-9]+/", "", $temp );
print_r($temp);
//outputs
Array
(
[0] => com
[1] => in
[2] => aus
[3] => cz
)
?>