/    Sign up×
Community /Pin to ProfileBookmark

How To Build My Own Validator: FILTER_VALIDATE_STRING ?

Folks,

Subject today are the VALIDATORS in the function filter_input().
So far, we got these built-into php:
A.

[code]
filter_input(INPUT_POST,”$input”,FILTER_VALIDATE_URL)
[/code]

B.

[code]
filter_input(INPUT_POST,”$input”,FILTER_VALIDATE_EMAIL)
[/code]

The above 2 VALIDATORS exist. But not this following one:

[code]
filter_input(INPUT_POST,”$input”,FILTER_VALIDATE_STRING)
[/code]

Yes, we have the STRING SANITIZER:

[code]
filter_input(INPUT_POST,”$input”,FILTER_SANITIZE_STRING)
[/code]

But I don’t want to sanitize the user’s (form inputer’s) input. I want to validate it.
So now trying to build my own FILTER_VALIDATE_STRING to use inside the filter_input() function.
I know there are other ways of validating a string but I want to learn how to build my own function or whatever to use it with the filter_input() function. That’s the assignment for now.
The following are my two attempts.
I get the following same error on both.

**Fatal error: Uncaught ArgumentCountError: Too few arguments to function FILTER_VALIDATE_STRING(), 0 passed in C:xampphtdocsTEST.php on line 97 and exactly 1 expected in C:xampphtdocsTemplatesTEST.php:84 Stack trace: #0 C:xampphtdocsTEST.php(97): FILTER_VALIDATE_STRING() #1 {main} thrown in C:xampphtdocsTEST.php on line …**

ATTEMPT 1.

[code]
<?php

$input = 123;

function FILTER_VALIDATE_STRING3($data)
{
if(is_string($data))
{
define(“FILTER_VALIDATE_STRING”,”STRING”);
}
else
{
define(“FILTER_VALIDATE_STRING”,”NO STRING”);
}
return $constant(“FILTER_VALIDATE_STRING”);
}

echo FILTER_VALIDATE_STRING();

if(!filter_input(INPUT_POST,”$input”,FILTER_VALIDATE_STRING))
{
die(“Enter a valid STRING!”);
}

?>
[/code]

ATTEMPT 2.

[code]
<?php

$input = 123;

function FILTER_VALIDATE_STRING($data)
{
if(is_string($data))
{
$constant = define(“FILTER_VALIDATE_STRING”,”STRING”);
}
else
{
$constant = define(“FILTER_VALIDATE_STRING”,”NO STRING”);
}
return $constant(“FILTER_VALIDATE_STRING”);
}

echo FILTER_VALIDATE_STRING();

if(!filter_input(INPUT_POST,”$input”,FILTER_VALIDATE_STRING4))
{
die(“Enter a valid STRING!”);
}

?>
[/code]

How to rid this problem ?

to post a comment
PHP

20 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmOct 28.2021 — 😆 😆 😆
Copy linkTweet thisAlerts:
@NogDogOct 28.2021 — > @developer_web#1638747 So now trying to build my own FILTER_VALIDATE_STRING to use inside the filter_input() function.

Those arguments are not function names, they are built-in PHP constants of type integer, which the filter_input() function then uses to determine which built-in function to use. The only way you could add a new one is to actually modify the C code used to build PHP, recompile it, and install that modified version of PHP on your web server. That ain't going to happen, I'm pretty sure.
Copy linkTweet thisAlerts:
@developer_webauthorOct 28.2021 — @NogDog#1638751

I was suggested to use FILTER_CALLBACK but the example here is no good:

https://www.php.net/manual/en/filter.filters.misc.php

Can you enlighten it with a code sample ?

Cheers!
Copy linkTweet thisAlerts:
@developer_webauthorOct 28.2021 — @ginerjm#1638749

Here, glad to sort of teach you something new:

https://www.w3schools.com/php/filter_callback.asp

That should drop the 3 smiles off your 3 heads.
Copy linkTweet thisAlerts:
@ginerjmOct 28.2021 — I have no interest in whatever it is you are doing. And no interest in anything w3schools has to say.
Copy linkTweet thisAlerts:
@developer_webauthorOct 28.2021 — @ginerjm#1638749

Here, glad to sort of teach you something new:

https://www.w3schools.com/php/filter_callback.asp

That should drop the 3 smiles off your 3 heads.

Ok. I understand my mistake ...
<i>
</i>
$input = '123';

if(!filter_input(INPUT_POST,"$input",FILTER_VALIDATE_STRING4))
Copy linkTweet thisAlerts:
@developer_webauthorOct 28.2021 — @NogDog#1638751

I tried the CALLBACK as suggested.

But it ain't working.

<i>
</i>&lt;form method="POST" name="textfield" id="textfield" action="" required&gt;
&lt;label for="name"&gt;Name:&lt;/label&gt;&lt;br&gt;
&lt;input type="text" name="name" id="name" maxlength="255" size="25"&gt;
&lt;br&gt;
&lt;button type="reset"&gt;Reset!&lt;/button&gt;
&lt;br&gt;
&lt;button type="submit" name="submit" id="submit"&gt;Submit!&lt;/button&gt;&lt;br&gt;
&lt;/form&gt;

&lt;?php
if($_SERVER['REQUEST_METHOD']==='POST')
{
$name = $_POST['name'];

<i> </i>function FILTER_VALIDATE_STRING6($data)
<i> </i>{
<i> </i> if(is_string($data))
<i> </i> {
<i> </i> $value = TRUE;
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> $value = FALSE;
<i> </i> }
<i> </i> GLOBAL $value;
<i> </i> return $value;
<i> </i>}
<i> </i>
<i> </i>echo FILTER_VALIDATE_STRING6($name);
<i> </i>if(!filter_input(INPUT_POST,"name",FILTER_CALLBACK,
<i> </i>"FILTER_VALIDATE_STRING6"))
<i> </i>{
<i> </i> die("Enter a valid STRING!");
<i> </i>}
}
?&gt;


**Warning: filter_input(): First argument is expected to be a valid callback in C:xampphtdocsTEST.php on line 189

Enter a valid STRING!**
Copy linkTweet thisAlerts:
@NogDogOct 28.2021 — I have never used filter_input() in over 20 years of PHP coding, and see no reason to start using it now. If you are able to write a function that validates whatever it is you want to validate, just call it directly -- I see zero reason to embed it within another function.
Copy linkTweet thisAlerts:
@developer_webauthorOct 28.2021 — @NogDog#1638759

Ok NogDog.

But you use filter_var() frequently and so let's try it with that. It's all about me getting to learn to use the CALLBACK function as it's new to me. Ok ?

Here's my attempt ...

<i>
</i>&lt;form method="POST" name="textfield" id="textfield" action="" required&gt;
&lt;label for="name"&gt;Name:&lt;/label&gt;&lt;br&gt;
&lt;input type="text" name="name" id="name" maxlength="255" size="25"&gt;
&lt;br&gt;
&lt;button type="reset"&gt;Reset!&lt;/button&gt;
&lt;br&gt;
&lt;button type="submit" name="submit" id="submit"&gt;Submit!&lt;/button&gt;&lt;br&gt;
&lt;/form&gt;

&lt;?php

if($_SERVER['REQUEST_METHOD']==='POST')
{
$name = $_POST['name'];

<i> </i>function FILTER_VALIDATE_STRING6($data)
<i> </i>{
<i> </i> if(is_string($data))
<i> </i> {
<i> </i> $value = TRUE;
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> $value = FALSE;
<i> </i> }
<i> </i> GLOBAL $value;
<i> </i> return $value;
<i> </i>}
<i> </i>
<i> </i>echo FILTER_VALIDATE_STRING6($name);

<i> </i>if(!filter_var($name,FILTER_CALLBACK,
<i> </i>"FILTER_VALIDATE_STRING6"))
<i> </i>{
<i> </i> die("Enter a valid STRING!");
<i> </i>}
}
?&gt;


Warning: filter_var(): First argument is expected to be a valid callback in C:xampphtdocsTEST.php on line ...
Copy linkTweet thisAlerts:
@NogDogOct 28.2021 — > @developer_web#1638760 But you use filter_var() frequently

Nope, don't use it, either.
Copy linkTweet thisAlerts:
@NogDogOct 28.2021 — $name doesn't exist, but $_POST['name'] might.
Copy linkTweet thisAlerts:
@developer_webauthorOct 28.2021 — @NogDog#1638762

But $name does exist. Look:
<i>
</i>if($_SERVER['REQUEST_METHOD']==='POST')
{
$name = $_POST['name'];


Anyway, did not make any difference. Still the error.
<i>
</i>if($_SERVER['REQUEST_METHOD']==='POST')
{
$name = $_POST['name'];

<i> </i>function FILTER_VALIDATE_STRING6($data)
<i> </i>{
<i> </i> if(is_string($data))
<i> </i> {
<i> </i> $value = TRUE;
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> $value = FALSE;
<i> </i> }
<i> </i> GLOBAL $value;
<i> </i> return $value;
<i> </i>}
<i> </i>
<i> </i>echo FILTER_VALIDATE_STRING6($name);

<i> </i>if(!filter_var($_POST['name'],FILTER_CALLBACK,
<i> </i>"FILTER_VALIDATE_STRING6"))
<i> </i>{
<i> </i> die("Enter a valid STRING!");
<i> </i>}
}
?&gt;

Warning: filter_var(): First argument is expected to be a valid callback in C:xampphtdocsTEST.php on line ...
Copy linkTweet thisAlerts:
@NogDogOct 28.2021 — That 3rd parameter to filter_var() needs to be an array. If we clean things up a bunch and try to K.I.S.S. (keep it simple, stupid), you could do:
[code=php]
<?php
// simulate POST request for this test:
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST['name'] = 'test';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if(!filter_var($_POST['name'], FILTER_CALLBACK, ["options" => "my_is_string"])) {
die("Enter a valid STRING!");
}
echo "'{$_POST['name']}' is a string.";
}

// ridiculously redundant function wrapper:
function my_is_string($data)
{
return is_string($data);
}
[/code]
Copy linkTweet thisAlerts:
@NogDogOct 28.2021 — PS: Note that ___everything___ in the $_POST array will be of type string, so this is all actually completely unnecessary for this specific test.
Copy linkTweet thisAlerts:
@developer_webauthorOct 28.2021 — @NogDog#1638773

Mmm. But is not this true for $_GET also ?

So, show me a simple example where it won't become unnecessary then to use all this.

I also notice on tutorials that the 3rd param of filter_input() is also an array.
<i>
</i>if(!filter__input(INPUT_POST,"name", FILTER_CALLBACK, ["options" =&gt; "my_is_string"])) {
die("Enter a valid STRING!");
}

But on both occassions (filter_var(), filter_input()), we see on tutorial examples, the 3rd param with the array:

FILTER_CALLBACK, ["options" => "my_is_string"])

But we do not see the array getting defined anywhere before the ''calling'' it line. How so ?
Copy linkTweet thisAlerts:
@developer_webauthorOct 30.2021 — @NogDog

Bump!
Copy linkTweet thisAlerts:
@developer_webauthorOct 30.2021 — @NogDog#1638773

Mmm. But is not this true for $_GET also ?

So, show me a simple example where it won't become unnecessary then to use all this.

I also notice on tutorials that the 3rd param of filter_input() is also an array.
<i>
</i>if(!filter__input(INPUT_POST,"name", FILTER_CALLBACK, ["options" =&gt; "my_is_string"])) {
die("Enter a valid STRING!");
}

But on both occassions (filter_var(), filter_input()), we see on tutorial examples, the 3rd param with the array:

FILTER_CALLBACK, ["options" => "my_is_string"])

But we do not see the array getting defined anywhere before the ''calling'' it line. How so ?
Copy linkTweet thisAlerts:
@NogDogOct 30.2021 — > @developer_web#1638836 do not see the array getting defined

Because in PHP version 7+ you can use the ["key" =&gt; "value", "key2" =&gt; "value2"] syntax to define an array, so it's defining it right when it calls the function.

> @developer_web#1638836 this true for $_GET also ?

Yes
Copy linkTweet thisAlerts:
@chavsurDec 01.2021 — i was also looking for the same, thanks for answers.[.](https://www.surveyzop.com/) [.](https://www.surveyzop.com/tellculvers/)
Copy linkTweet thisAlerts:
@itzeazy21Dec 02.2021 — [Nice ](https://www.itzeazy.com/ownership-transfer-of-car-bike-delhi-1.html)detailed information
×

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 3.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: @darkwebsites540,
tipped: article
amount: 10 SATS,

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

tipper: Anonymous,
tipped: article
amount: 10 SATS,
)...