Skip to content
Advertisement

How to remove any special character from php array?

How to remove any special character from php array?

I have array like:

$temp = array (".com",".in",".au",".cz");

I want result as:

$temp = array ("com","in","au","cz");

I got result by this way:

$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.

SEE DEMO

<?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
)

?>
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement