Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to quickly retrieve tags in array from string?

I have $_GET['tags'] = "apples, oranges, bananas, grapes, cherries"

I need to place the data into an array ($tags).

What is a quick way to trim each item and perform security functions (stripping html, special chars)?

like image 372
Alex L Avatar asked Aug 01 '26 20:08

Alex L


1 Answers

With array_walk() you could write your tag cleaning function separately, and then easily apply it to your incoming data.

function sterilize(&$val,$key)
{
    //do whatever security you need here
    $val = trim($val);
    $val = strip_tags($val);
    //etc
    return htmlspecialchars($val);
}
$bad_values = explode(',',$_GET['tags']);
array_walk($bad_values,'sterilize');
like image 77
zombat Avatar answered Aug 03 '26 09:08

zombat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!