The PHP Filter Extension

Posted on Updated on

The PHP Filter Extension

PHP channels are utilized to approve and clean outside information. The PHP channel augmentation has a large number of the capacities required for checking client enter, and is intended to make information approval less demanding and faster.

The filter_list( ) capacity can be utilized to rundown what the PHP channel augmentation offers:

Sample

< table >

< tr >

< td > Filter Name < / td >

< td > Filter ID < / td >

< / tr >

< ?php

foreach (filter_list( ) as $ id = > $ filter ) {

reverberation “ < tr > < td > “ . $ filter . ‘ < / td > < td > ‘ .

filter_id( $ filter ) . ‘ < / td > < / tr > ‘ ;

}

? >

< / table >

________________________________________

Why Use Filters?

Numerous web applications get outer info. Outside information / information can be:

• User info from a structure

• Cookies

• Web administrations information

• Server variables

• Database inquiry results

________________________________________

PHP filter_var( ) Function

The filter_var( ) capacity both accept and sterilize information.

The filter_var( ) capacity channels a solitary variable with a

Predefined channel. It takes two bits of information:

• The variable you need to check

• The sort of check to utilize

________________________________________

Disinfect a String

The accompanying illustration utilizes the filter_var( ) capacity to expel all HTML labels from a string:

Illustration

< ?php

$ str = “ < h1 > Hello World! < / h1 > “ ;

$ newstr = filter_var( $ str, FILTER_SANITIZE_STRING ) ;

reverberation $ newstr;

? >

________________________________________

Accept an Integer

The accompanying illustration utilizes the filter_var( ) capacity to check if the variable $ int is a whole number. In the event that $ int is a whole number, the yield of the code above will be: “ Whole number is substantial “ . In the event that $ int is not a whole number, the yield will be: “ Number is not legitimate “ :

Case

< ?php

$ int = 100;

in the event that (!filter_var( $ int, FILTER_VALIDATE_INT ) ===

false ) {

echo( “ Integer is legitimate “ ) ;

}

else {

echo( “ Integer is not legitimate “ ) ;

}

? >

Tip: filter_var( ) and Problem With 0

In the case above, if $ int was set to 0, the capacity above will return “ Number is not substantial “ . To take care of this issue, utilize the code beneath:

Illustration

< ?php

$ int = 0;

on the off chance that (filter_var( $ int, FILTER_VALIDATE_INT ) ===

0 || !filter_var( $ int, FILTER_VALIDATE_INT ) === false ) {

echo( “ Integer is substantial “ ) ;

} else {

echo( “ Integer is not substantial “ ) ;

}

? >

________________________________________

Approve an IP Address

The accompanying illustration utilizes the filter_var( ) capacity to check if the variable $ ip is a substantial IP address:

Sample

< ?php

$ ip = “ 127.0.0.1 “ ;

on the off chance that (!filter_var( $ ip, FILTER_VALIDATE_IP ) ===

false ) {

echo( “ $ ip is a substantial IP address “ ) ;

} else {

echo( “ $ ip is not a substantial IP address “ ) ;

}

? >

________________________________________

Sterilize and Validate an Email Address

The accompanying sample utilizes the filter_var( ) capacity to first expel every illicit character from the $ email variable, then check in the event that it is a legitimate email address:

Sample

< ?php

$ email = “ john.doe@example.com “ ;

/ Remove every single unlawful character from email

$ email = filter_var( $ email, FILTER_SANITIZE_EMAIL ) ;

/ Validate email

in the event that (!filter_var( $ email, FILTER_VALIDATE_EMAIL )

=== false ) {

echo( “ $ email is a legitimate email address “ ) ;

} else {

echo( “ $ email is not a legitimate email address “ ) ;

}

? >

________________________________________

Purify and Validate a URL

The accompanying illustration utilizes the filter_var( ) capacity to first expel every illicit character from a URL, then check if $ url is a legitimate URL:

Illustration

< ?php

$ url = “ http: / / http://www.w3schools.com “ ;

/ Remove every single illicit character from a url

$ url = filter_var( $ url, FILTER_SANITIZE_URL ) ;

/ Validate url

on the off chance that (!filter_var( $ url, FILTER_VALIDATE_URL )

=== false ) {

echo( “ $ url is a substantial URL “ ) ;

} else {

echo( “ $ url is not a substantial Url “ ) ;

}

? >

For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.

Helpline: 9814666333, 8699444666

Email:info@technocampus.co.in

**************************************************************************************************************

Please fill the FORM and we shall contact you soon.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s