PHP:将标记从array_values()中的值剥离

问题描述:

我想在使用制表符爆裂之前将标签从array_values()中的值中剥离出来。

I want to strip the tags off the value inside array_values() before imploding with tabs.

我在下面尝试了此行,但出现错误,

I tried with this line below but I have an error,

$output = implode("\t",strip_tags(array_keys($item)));

理想情况下,我想从值中删除换行符,双精度空格,制表符

ideally I want to strip off the line breaks, double spaces, tabs from the value,

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

但我认为我的方法不正确!

but I think my method is not correct!

这是整个函数,

function process_data($items){

    # set the variable
    $output = null;

    # check if the data is an items and is not empty
    if (is_array($items)  && !empty($items))
    {
        # start the row at 0
        $row = 0;

        # loop the items
        foreach($items as $item)
        {
            if (is_array($item) && !empty($item))
            {
                if ($row == 0)
                {
                    # write the column headers
                    $output = implode("\t",array_keys($item));
                    $output .= "\n";
                }

                # create a line of values for this row...
                $output .= implode("\t",array_values($item));
                $output .= "\n";

                # increment the row so we don't create headers all over again
                $row++;
            }
        }       
    }

    # return the result
    return $output;
}

请告诉我您是否有解决办法。谢谢!

Please let me know if you have any ideas how to fix this. Thanks!

strip_tags 仅适用于字符串,不适用于数组输入。因此,必须在内爆输入字符串后应用它。

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

您必须测试它是否可以提供所需的结果。我不知道preg_replace会完成什么。

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

否则,您可以使用 array_map( strip_tags,array_keys($ item))首先删除标签(如果字符串中的标签中确实有任何重要的 \t 。)

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