views:

125

answers:

5

i have an array like this

Array
(
    [0] => "<[email protected]>"
    [1] => "<[email protected]>"
    [2]=>  "<[email protected]>"
)

now i want to remove "<" and ">" from above array so that it look like

Array
(
    [0] =>  [email protected]
    [1] =>  [email protected]
    [2] =>  [email protected]
)

how to do this in php? please help me out

I'm using array_filter(), is there any easier way to do that except array_filter().

+4  A: 

Firstly, if you want to change values it's array_map() you want, not array_filter(). array_filter() selectively removes or keeps array entries.

$output = array_map('remove_slashes', $input);

function remove_slashes($s) {
  return preg_replace('!(^<|>$)!', '', $s);
}

You could of course do this with a simple foreach loop too.

cletus
Aint these called *angle brackets*?
Gordon
+7  A: 

You could take an array_walk on it:

// Removes starting and trailing < and > characters

 function trim_gt_and_lt(&$value) 
{ 
    $value = trim($value, "<>"); 
}

array_walk($array, 'trim_gt_and_lt');

Note however that this will also remove starting > and trailing < which may not be what you want.

Pekka
Best solution. That's how I'd have done it, too.
Franz
This also will strip leading > and trailing <. Is that what the OP wants?
cletus
You could probably even do it with an anonymous function in array_walk(), but I can't do the looking up and testing right now.
Pekka
@cletus, good point. I assume in his data set it's irrelevant but still. +1 to your solution and added a caveat to mine.
Pekka
Actually, maybe you could use `str_replace` instead of `trim`, just in case the brackets could be somewhere else (not that I really believe that)...
Franz
Its an important caveat but honestly I didn't even know you could do that with `trim()` so +1 for that.
cletus
It is not working Dear.....it does not strip even a single tag..may be i m wrong or u didn't understand my problem
diEcho
Works perfectly for me, strips every occurrence of `<` and `>` from the array.
Pekka
@Franz str_replace would do this. I have posted my reply which shows how to do this using str_replace
AntonioCS
I know it does. That's why I mentioned it ;)
Franz
+1  A: 

str_replace is an option, or any other replacement functions in PHP like preg_replace etc.

Residuum
A: 

you go through the array and do it one by one?

$arr = array( "<[email protected]>", "<[email protected]>" ,"<[email protected]>");
foreach ($arr as $k=>$v){
    $arr[$k] = trim($v,"<>") ;
}
print_r($arr);

output

$ php test.php
Array
(
    [0] => [email protected]
    [1] => [email protected]
    [2] => [email protected]
)
ghostdog74
have u apply this function or just wrire the output what i want
diEcho
what do you mean? as you can see from my output, it does what you want
ghostdog74
A: 

Why not just use str_replace

$teste = array("<[email protected]>","<[email protected]>","<[email protected]>");
var_dump(str_replace(array('<','>'),'',$teste));

Will print out

array
  0 => string '[email protected]' (length=11)
  1 => string '[email protected]' (length=13)
  2 => string '[email protected]' (length=17)
AntonioCS