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

lau*_*kok 7 php arrays preg-replace implode strip-tags

我想在使用制表符进行内爆之前从array_values()内部的值中删除标记.

我试过下面的这一行,但是我有一个错误,

$output = implode("\t",strip_tags(array_keys($item)));
Run Code Online (Sandbox Code Playgroud)

理想情况下,我想剥离换行符,双倍空格,值的标签,

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

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

这是整个功能,

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;
}
Run Code Online (Sandbox Code Playgroud)

如果您有任何想法如何解决这个问题,请告诉我.谢谢!

mar*_*rio 3

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

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

您必须测试它是否能给您带来所需的结果。我不知道 preg_replace 的作用是什么。

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

(不知道你的大功能是什么。)