开发者

remove "<" and ">" tags from an array in php

开发者 https://www.devze.com 2022-12-17 08:10 出处:网络
I have an array like this: Array ( [0] => \"<one@one.com>\" [1] => \"<two@two.co.in>\" [2] => \"<three@hello.co.in>\"

I have an array like this:

Array
(
    [0] => "<one@one.com>"
    [1] => "<two@two.co.in>"
    [2] => "<three@hello.co.in>"
)

Now I want to r开发者_JAVA百科emove "<" and ">" from above array so that it look like

Array
(
    [0] => "one@one.com"
    [1] => "two@two.co.in"
    [2] => "three@hello.co.in"
)

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


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.


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.


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


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

$arr = array( "<one@one.com>", "<two@two.co.in>" ,"<three@hello.co.in>");
foreach ($arr as $k=>$v){
    $arr[$k] = trim($v,"<>") ;
}
print_r($arr);

output

$ php test.php
Array
(
    [0] => one@one.com
    [1] => two@two.co.in
    [2] => three@hello.co.in
)


Why not just use str_replace

$teste = array("<one@one.com>","<two@two.co.in>","<three@hello.co.in>");
var_dump(str_replace(array('<','>'),'',$teste));

Will print out

array
  0 => string 'one@one.com' (length=11)
  1 => string 'two@two.co.in' (length=13)
  2 => string 'three@hello.co.in' (length=17)
0

精彩评论

暂无评论...
验证码 换一张
取 消