5

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.

1

4 Answers 4

17

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
)

?>
Sign up to request clarification or add additional context in comments.

4 Comments

Replace with an empty string, not with a space
yea exactly... empty string should be pass in function otherwise it returns as " com".
@swap7927 You're welcome. I was happy to do it. I know you'll do the same for someone else
Note; technically, you only want to REMOVE the periods, not only ADD letters and numbers. Therefore, the most basic preg_replace answer should show $temp= preg_replace('/\./', '', $temp);
5

I generaly make a function

function make_slug($data)
     {
         $data_slug = trim($data," ");
         $search = array('/','\\',':',';','!','@','#','$','%','^','*','(',')','_','+','=','|','{','}','[',']','"',"'",'<','>',',','?','~','`','&',' ','.');
         $data_slug = str_replace($search, "", $data_slug);
         return $data_slug;
     }

And then call it in this way

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


for($i = 0; $i<count($temp); $i++)
{
    $temp[$i] = make_slug($temp[$i]);
}

print_r($temp);

Each value of $temp will then become free of special characters

See the Demo

Comments

0

As a solution to your problem please execute the following code snippet

    $temp = array (".com",".in",".au",".cz");
    function strip_special_chars($v)
    {
        return str_replace('.','',$v);
    }
    $result[]=array_map('strip_special_chars',$temp);

1 Comment

check ur code before posting ans. it does not work.str_replace works for string only not php array.
0

Actually trim() can trim any character(s) you wish if you provide the 2nd argument (character mask). As it's name implies, this will only remove characters from the beginning and end of the string. In your case ltrim() may be more appropriate.

You can use array_map() and ltrim() together with a third parameter for the character mask like this:

$temp = array_map('ltrim', $temp, array_fill(0, count($temp), '.'));

The third parameter should be an array of arguments matching the length of the array you're processing, which is why I used array_fill() to create it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.