{"id":2772,"date":"2021-09-28T14:28:35","date_gmt":"2021-09-28T14:28:35","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=2772"},"modified":"2021-09-28T14:32:34","modified_gmt":"2021-09-28T14:32:34","slug":"php-filter","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-tutorial\/php-filter\/","title":{"rendered":"PHP filter"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn to define a PHP <code>filter()<\/code> function that sanitizes and validates data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='define-php-filter-function'>Define PHP filter() function <a href=\"#define-php-filter-function\" class=\"anchor\" id=\"define-php-filter-function\" title=\"Anchor for Define PHP filter() function\">#<\/a><\/h2>\n\n\n\n<p>In the previous tutorials, you learned how to define the <code><a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-sanitize-input\/\">sanitize()<\/a><\/code> and <code><a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-validation\/\">validate()<\/a><\/code> functions to sanitize and validate data.<\/p>\n\n\n\n<p>The <code>sanitize()<\/code> function sanitizes data based on specified filters and returns an array that contains the sanitized data. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$inputs = sanitize($_POST, &#91;\n    <span class=\"hljs-string\">'name'<\/span> =&gt; <span class=\"hljs-string\">'string'<\/span>,\n    <span class=\"hljs-string\">'email'<\/span> =&gt; <span class=\"hljs-string\">'email'<\/span>\n]);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>validate()<\/code> function validates data based on the rules and returns an array that contains the error messages:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$errors = validate($inputs,&#91;\n    <span class=\"hljs-string\">'name'<\/span> =&gt; <span class=\"hljs-string\">'required | max: 255'<\/span>,\n    <span class=\"hljs-string\">'email'<\/span> =&gt; <span class=\"hljs-string\">'required | email'<\/span>,\n]);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>These functions work fine. However, you need to specify two sets of rules: sanitization and validation rules.<\/p>\n\n\n\n<p>To make them more concise, you can define a <code>filter()<\/code> function that both sanitizes and validates data based on the combination of the sanitization and validation rules:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">filter<\/span><span class=\"hljs-params\">(array $data, array $fields, array $messages=&#91;])<\/span>: <span class=\"hljs-title\">array<\/span>\n<\/span>{\n    <span class=\"hljs-comment\">\/\/ implementation<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">&#91;$inputs, $errors] = filter($_POST, &#91;\n    <span class=\"hljs-string\">'name'<\/span> =&gt; <span class=\"hljs-string\">'string | required | max: 255'<\/span>,\n    <span class=\"hljs-string\">'email'<\/span> =&gt; <span class=\"hljs-string\">'email | required | email'<\/span>,\n]);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>name<\/code> field has the <code>string<\/code> filter rule and the <code>required | max: 255<\/code> validation rule in this code. Therefore, you need to extract the filter and validation rules from the $rules.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$sanitization_rules = &#91;];\n$validation_rules = &#91;];\n\n<span class=\"hljs-keyword\">foreach<\/span> ($fields <span class=\"hljs-keyword\">as<\/span> $field =&gt; $rules) {\n    <span class=\"hljs-keyword\">if<\/span> (strpos($rules, <span class=\"hljs-string\">'|'<\/span>)) {\n        &#91;$sanitization_rules&#91;$field], $validation_rules&#91;$field] ] =  explode(<span class=\"hljs-string\">'|'<\/span>, $rules, <span class=\"hljs-number\">2<\/span>);\n    } <span class=\"hljs-keyword\">else<\/span> {\n            $sanitization_rules&#91;$field] = $rules;\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, define two arrays that hold the sanitization and validation rules:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$sanitization_rules = &#91;];\n$validation_rules = &#91;];<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, iterate over the <code>$fields<\/code> array. For each element, if the <code>$rules<\/code> contains the <code>|<\/code> character, split the <code>$rules<\/code> string using the <code>|<\/code> separator into two and assign the first element to<code>$sanitization_rules[$field]<\/code>and the second element to<code>$validation_rules[$field]<\/code>. Otherwise, assign the $rules to the <code>$sanitization_rules[$field]<\/code>.<\/p>\n\n\n\n<p>For example, if you have the following fields:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">&#91;\n    <span class=\"hljs-string\">'name'<\/span> =&gt; <span class=\"hljs-string\">'string | required | max: 255'<\/span>,\n    <span class=\"hljs-string\">'email'<\/span> =&gt; <span class=\"hljs-string\">'email | required | email'<\/span>,\n]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>$sanitization_rules<\/code> will be:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"> &#91;\n    <span class=\"hljs-string\">'name'<\/span> =&gt; <span class=\"hljs-string\">'string'<\/span>,\n    <span class=\"hljs-string\">'email'<\/span> =&gt; <span class=\"hljs-string\">'email'<\/span>,\n]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And the <code>validation_rules<\/code> will be:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">&#91;\n    <span class=\"hljs-string\">'name'<\/span> =&gt; <span class=\"hljs-string\">'required | max: 255'<\/span>,\n    <span class=\"hljs-string\">'email'<\/span> =&gt; <span class=\"hljs-string\">'required | email'<\/span>,\n]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Once having the sanitization and validation rules, you can call the <code>sanitize()<\/code> and <code>validate()<\/code> function in sequence and returns an array that contains the sanitized inputs and validation errors:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">\/\/ ...<\/span>\n$inputs = sanitize($data, $sanitization_rules);\n$errors = validate($inputs, $validation_rules, $messages);\n\n<span class=\"hljs-keyword\">return<\/span> &#91;$inputs, $errors];<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here&#8217;s the complete <code>filter()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">filter<\/span><span class=\"hljs-params\">(array $data, array $fields, array $messages=&#91;])<\/span> : <span class=\"hljs-title\">array<\/span>\n<\/span>{\n    $sanitization_rules = &#91;];\n    $validation_rules  = &#91;];\n\n    <span class=\"hljs-keyword\">foreach<\/span> ($fields <span class=\"hljs-keyword\">as<\/span> $field=&gt;$rules) {\n        <span class=\"hljs-keyword\">if<\/span> (strpos($rules, <span class=\"hljs-string\">'|'<\/span>)) {\n            &#91;$sanitization_rules&#91;$field], $validation_rules&#91;$field] ] =  explode(<span class=\"hljs-string\">'|'<\/span>, $rules, <span class=\"hljs-number\">2<\/span>);\n        } <span class=\"hljs-keyword\">else<\/span> {\n            $sanitization_rules&#91;$field] = $rules;\n        }\n    }\n\n    $inputs = sanitize($data, $sanitization_rules);\n    $errors = validate($inputs, $validation_rules, $messages);\n\n    <span class=\"hljs-keyword\">return<\/span> &#91;$inputs, $errors];\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='use-the-php-filter-function'>Use the PHP filter() function <a href=\"#use-the-php-filter-function\" class=\"anchor\" id=\"use-the-php-filter-function\" title=\"Anchor for Use the PHP filter() function\">#<\/a><\/h2>\n\n\n\n<p>The following example shows how to use the <code>filter()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">require<\/span> <span class=\"hljs-keyword\">__DIR__<\/span> . <span class=\"hljs-string\">'\/filter.php'<\/span>;\n\n$data = &#91;\n    <span class=\"hljs-string\">'name'<\/span> =&gt; <span class=\"hljs-string\">''<\/span>,\n    <span class=\"hljs-string\">'email'<\/span> =&gt; <span class=\"hljs-string\">'john$email.com'<\/span>,\n];\n\n$fields = &#91;\n    <span class=\"hljs-string\">'name'<\/span> =&gt; <span class=\"hljs-string\">'string | required | max: 255'<\/span>,\n    <span class=\"hljs-string\">'email'<\/span> =&gt; <span class=\"hljs-string\">'email | required | email'<\/span>\n];\n\n&#91;$inputs, $errors] = filter($data, $fields);\n\nprint_r($inputs);\nprint_r($errors);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">Array<\/span>\n(\n    &#91;name] =&gt; Please enter the name\n    &#91;email] =&gt; The email is not a valid email address\n)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Use the PHP <code>filter()<\/code> helper function to sanitize and validate data.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"2772\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-filter\/\"\n\t\t\t\tdata-post-title=\"PHP filter\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"2772\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-filter\/\"\n\t\t\t\tdata-post-title=\"PHP filter\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn to define a PHP filter() function that sanitizes and validates data.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":15,"menu_order":99,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2772","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/2772","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=2772"}],"version-history":[{"count":2,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/2772\/revisions"}],"predecessor-version":[{"id":2774,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/2772\/revisions\/2774"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/15"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=2772"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}