/    Sign up×
Community /Pin to ProfileBookmark

The filter_var() 3rd Option Question

Folks,

Look what Manual says confusing the daylight out of me:

**options

Associative array of options or bitwise disjunction of flags. If filter accepts options, flags can be provided in “flags” field of array. For the “callback” filter, callable type should be passed. The callback must accept one argument, the value to be filtered, and return the value after filtering/sanitizing it.**

Then shows this adv level code and turns my world upside down and ruins it:

[code]

<?php
// for filters that accept options, use this format
$options = array(
‘options’ => array(
‘default’ => 3, // value to return if the filter fails
// other options here
‘min_range’ => 0
),
‘flags’ => FILTER_FLAG_ALLOW_OCTAL,
);
$var = filter_var(‘0755’, FILTER_VALIDATE_INT, $options);

// for filters that only accept flags, you can pass them directly
$var = filter_var(‘oops’, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);

// for filters that only accept flags, you can also pass as an array
$var = filter_var(‘oops’, FILTER_VALIDATE_BOOLEAN,
array(‘flags’ => FILTER_NULL_ON_FAILURE));

// callback validate filter
function foo($value)
{
// Expected format: Surname, GivenNames
if (strpos($value, “, “) === false) return false;
list($surname, $givennames) = explode(“, “, $value, 2);
$empty = (empty($surname) || empty($givennames));
$notstrings = (!is_string($surname) || !is_string($givennames));
if ($empty || $notstrings) {
return false;
} else {
return $value;
}
}
$var = filter_var(‘Doe, Jane Sue’, FILTER_CALLBACK, array(‘options’ => ‘foo’));
?>

[/code]

https://www.php.net/manual/en/function.filter-var.php

Can’t be dealing with that ugly mess.
So, who can explain the “option” 3rd parameter to me in Layman’s English with a beginner level code sample ?
Talking about this function: filter_var.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@developer_webauthorApr 18.2021 — @NogDog,

Don't like dragging you up like this but do I have any choice ?
Copy linkTweet thisAlerts:
@developer_webauthorApr 22.2021 — @NogDog,

Care to address this thread ?

Thanks.
×

Success!

Help @developer_web spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...