简体   繁体   English

从php中的数组中删除“<”和“>”标签

[英]remove “<” and “>” tags from an array in php

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 remove "<" 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? 如何在PHP中执行此操作? Please help me out. 请帮帮我。

I'm using array_filter() ; 我正在使用array_filter() ; is there any easier way to do that except array_filter() ? 除了array_filter()之外,还有更简单的方法吗?

You could take an array_walk on it: 你可以在上面使用array_walk:

// 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_map() ,而不是array_filter() array_filter() selectively removes or keeps array entries. array_filter()有选择地删除或保留数组条目。

$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. 当然,您也可以使用简单的foreach循环来完成此操作。

str_replace是一个选项,或PHP中的任何其他替换函数,如preg_replace等。

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 为什么不使用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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM