{"id":365,"date":"2018-01-14T17:49:29","date_gmt":"2018-01-14T17:49:29","guid":{"rendered":"http:\/\/mddir.com\/how-to\/?p=365"},"modified":"2023-04-16T12:55:31","modified_gmt":"2023-04-16T12:55:31","slug":"php-filter-extension","status":"publish","type":"post","link":"https:\/\/www.mddir.com\/how-to\/php-filter-extension\/","title":{"rendered":"PHP Filter Extension"},"content":{"rendered":"\n<a href=\"https:\/\/flowerboomdallas.com\/\" target=\"_blank\" rel=\"noopener\">flower in dallas<\/a>\n\n\n<p>The php filters are used to validate and sanitize the unidentified user input. Use filter_list() function to list the php filter extensions.<\/p>\n<p><code>PHP filter_var() Function<\/code><\/p>\n<p>The <code>filter_var() function<\/code> is used to validate and sanitize data.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre>[php]&amp;amp;lt;?php\n$str = &quot;&amp;amp;lt;h1&amp;amp;gt;Hello World!&amp;amp;lt;\/h1&amp;amp;gt;&quot;;\n$newstr = filter_var($str, FILTER_SANITIZE_STRING);\necho $newstr;\n?&amp;amp;gt;[\/php]<\/pre>\n<p><code>Code Explanation:<\/code><\/p>\n<p>In the above code we have declared the variable <code>$str<\/code> and provided the value <code><\/code><\/p>\n<p><code><code><\/code><\/code><\/p>\n<h1>Hello World!<\/h1>\n<p><code>\n<\/code><\/p>\n<p><code><\/code>.<\/p>\n<p>On the next php line, we have created a new variable called <code>$newstr<\/code> and created a <code>filter_var()<\/code> function. The filter function we are calling <code>$str<\/code> and using the filter statement <code>FILTER_SANITIZE_STRING<\/code>. This statement removes the HTML code from the string. Then we echo the <code>$newstr<\/code> variable to get the result.<\/p>\n<p>The above code generates this result.<\/p>\n<p><code>Hello World!<\/code><\/p>\n<p><strong>Validate an Integer<\/strong><\/p>\n<p>Use <code>filter_var()<\/code> function to check if the value is integer or not.<\/p>\n<p><strong>Example Code:<\/strong><\/p>\n<pre>[php]&amp;amp;lt;?php\n$int = 100;\n\nif (!filter_var($int, FILTER_VALIDATE_INT) === false) {\n    echo(&quot;Integer is valid&quot;);\n} else {\n    echo(&quot;Integer is not valid&quot;);\n}\n?&amp;amp;gt;[\/php]<\/pre>\n<p><strong>Code Explanation:<\/strong><\/p>\n<p>In the above code, we created a variable $int and provided value 100. Then by declaring the if statement we are using filter statement to check the variable is an integer or not by using statement called FILTER_VALIDATE_INT. If the value is not an integer and not identical then it will show the false result.<\/p>\n<p>In the above code, the integer is valid so the outcome will look like this.<\/p>\n<p><strong>Integer is valid<\/strong><\/p>\n<p>Sanitize and Validate an Email Address<\/p>\n<p>In this example, the <code>filter_var()<\/code> function removes the illegal details from the email id and returns correct email.<\/p>\n<p><strong>Example Code:<\/strong><\/p>\n<pre>[php]&amp;amp;lt;?php\n$email = &quot;john.doe@\/\/((example.com&quot;;\n\n\/\/ Remove all illegal characters from email\n$email = filter_var($email, FILTER_SANITIZE_EMAIL);\n\n\/\/ Validate e-mail\nif (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {\n    echo(&quot;$email is a valid email address&quot;);\n} else {\n    echo(&quot;$email is not a valid email address&quot;);\n}\n?&amp;amp;gt;[\/php]<\/pre>\n<p><strong>Code Example:<\/strong><\/p>\n<p>The above code we have intentionally added to additional symbols in the email id. Our email id looks like this &#8220;<code>john.doe@\/\/((example.com<\/code>&#8221; which indicates the wrong format of using email id.<\/p>\n<p>By using the filter_var() function and the FILTER_VALIDATE_EMAIL statement we can remove the illegal characters from the id.<\/p>\n<p>The above code will show the result.<\/p>\n<p><code>john.doe@example.com is a valid email address<\/code><\/p>\n<p>The filter function has removed the illegal symbols from the email id.<\/p>\n<p><strong>Validate the URLs<\/strong><\/p>\n<p>Similarly you can use the use the filter function to validate the URLs.<\/p>\n<p><strong>Example<\/strong><\/p>\n<pre>[php]&amp;amp;lt;?php\n$url = &quot;https:\/\/www.w3schools.com&quot;;\n\n\/\/ Remove all illegal characters from a url\n$url = filter_var($url, FILTER_SANITIZE_URL);\n\n\/\/ Validate url\nif (!filter_var($url, FILTER_VALIDATE_URL) === false) {\n    echo(&quot;$url is a valid URL&quot;);\n} else {\n    echo(&quot;$url is not a valid URL&quot;);\n}\n?&amp;amp;gt;[\/php]<\/pre>\n<p>The above will show the result as below.<\/p>\n<p><code>https:\/\/www.w3schools.com is a valid URL<\/code><\/p>","protected":false},"excerpt":{"rendered":"<p>flower in dallas The php filters are used to validate and sanitize the unidentified user input. Use filter_list() function to list the php filter extensions.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-365","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/365","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/comments?post=365"}],"version-history":[{"count":3,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/365\/revisions"}],"predecessor-version":[{"id":847,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/365\/revisions\/847"}],"wp:attachment":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/media?parent=365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/categories?post=365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/tags?post=365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}