DEV Community

Nasrul Hazim Bin Mohamad
Nasrul Hazim Bin Mohamad

Posted on

5 3

Nested To HTML Entities

A quick one:

<?php 

if(! function_exists('to_htmlentities'))
{
    function to_htmlentities(array $data)
    {
        return collect($data)
            ->map(function($value) {
                if(is_string($value)) {
                    return htmlentities($value);
                }

                if(is_array($value)) {
                    return to_htmlentities($value);
                }

                return $value;
            })
            ->toArray();
    }
}
Enter fullscreen mode Exit fullscreen mode

Usage:

$data = [
    "<script>alert('hi')</script>",
    [
        "<script>alert('hello')</script>",
    ]
];

$data = to_htmlentities($data);
Enter fullscreen mode Exit fullscreen mode

This piece of code simply handle all text in array, including the nested one.

Top comments (0)

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay