2

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

2
  • Uhmmm... what do you want to remove from the first array? To me it's empty... Commented Jan 19, 2010 at 12:34
  • Never mind. Pekka just edited it. Commented Jan 19, 2010 at 12:36

5 Answers 5

7

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.

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

9 Comments

This also will strip leading > and trailing <. Is that what the OP wants?
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.
@cletus, good point. I assume in his data set it's irrelevant but still. +1 to your solution and added a caveat to mine.
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)...
Its an important caveat but honestly I didn't even know you could do that with trim() so +1 for that.
|
5

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.

1 Comment

Aint these called angle brackets?
1

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

Comments

0

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

2 Comments

have u apply this function or just wrire the output what i want
what do you mean? as you can see from my output, it does what you want
0

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)

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.