PHP: strip the tags off the value inside array_values()

拈花ヽ惹草 提交于 2019-12-05 05:18:12

strip_tags only works on strings, not on array input. Thus you have to apply it after implode made a string of the input.

$output = strip_tags(
    implode("\t",
        preg_replace(
           array("/\t/", "/\s{2,}/", "/\n/"),
           array("", " ", " "),
           array_keys($item)
        )
    )
);

You'll have to test if it gives you the desired results. I don't know what the preg_replace accomplishes.

Otherwise you could use array_map("strip_tags", array_keys($item)) to have the tags removed first (if there are really any significant \t within the tags in the strings.)

(No idea what your big function is about.)

try mapping the arrays to strip_tags and trim.

implode("\t", array_map("trim", array_map("strip_tags", array_keys($item))));
GolezTrol

Stripping the tags is easy as this:

$a = array('key'=>'array item<br>');

function fix(&$item, $key)
{
    $item = strip_tags($item);
}

array_walk($a, 'fix');

print_r($a);

Of course, you can make whatever modifications you like to $item in the fix function. The change will be stored in the array.

For a multidimensional array use array_walk_recursive($a, 'fix');.

Looks like you just need to use array_map, since strip_tags expects a string, not an array.

$arr = array(   "Some\tTabbed\tValue" => '1',
                "Some  value  with  double  spaces" => '2',
                "Some\nvalue\nwith\nnewlines" => '3',
            );

$search = array("#\t#", "#\s{2,}#", "#\n#");
$replace = array("", " ", " ");
$output = implode("\t", preg_replace($search, $replace, array_map('strip_tags', array_keys($arr))));
echo $output;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!