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

9.4k Views Asked by At

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!

this is the entire function,

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!

4

There are 4 best solutions below

1
On BEST ANSWER

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

3
On

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

0
On

try mapping the arrays to strip_tags and trim.

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

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;