Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3949220
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:28:03+00:00 2026-05-20T01:28:03+00:00

I want to refactor this piece of code, it takes input from a form,

  • 0

I want to refactor this piece of code, it takes input from a form, then sanitizes the input, then it checks if its empty, or too short. It does this for title, content and tags. It stores an errors encountered in an array called errors.

I want to make a function, something like this:

function validate_input($args)

Except I’m unsure as to how I’m going to implement it, and how it’ll build up an error list.

(I know I can use something like PEAR QUICKFORM or php-form-builder-class, so please don’t mention ‘oh use Class xyz’).

   $title = filter_input(INPUT_POST, 'thread_title',  FILTER_SANITIZE_STRING,
                                                      array('flags' => FILTER_FLAG_STRIP_HIGH|FILTER_FLAG_STRIP_LOW )); 
   $content = filter_input(INPUT_POST, 'thread_content');
   $tags = filter_input(INPUT_POST, 'thread_tags');

   #  title here:
   if (is_null($title) || $title == "")   #  is_null on its own returns false for some reason
   {
      $errors['title'] = "Title is required.";
   }
   elseif ($title === false)
   {
      $errors['title'] = "Title is invalid.";
   }
   elseif (strlen($title) < 15)
   {
      $errors['title'] = "Title is too short, minimum is 15 characters (40 chars max).";
   }
   elseif (strlen($title) > 80 )
   {
      $errors['title'] = "Title is too long, maximum is 80 characters.";
   }

   #  content starts here:
   if (is_null($content) || $content == "")
   {
      $errors['content'] = "Content is required.";
   }
   elseif ($content === false)
   {
      $errors['content'] = "Content is invalid.";
   }
   elseif (strlen($content) < 40)
   {
      $errors['content'] = "Content is too short, minimum is 40 characters.";    #  TODO: change all min char amounts
   }
   elseif (strlen($content) > 800)
   {
      $errors['content'] = "Content is too long, maximum is 800 characters.";
   }

   #  tags go here:
   if (is_null($tags) || $tags == "")
   {
      $errors['tags'] = "Tags are required.";
   }
   elseif ($title === false)
   {
      $errors['tags'] = "Content is invalid.";
   }
   elseif (strlen($tags) < 3)
   {
      $errors['tags'] = "Atleast one tag is required, 3 characters long.";
   }

   var_dump($errors);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T01:28:04+00:00Added an answer on May 20, 2026 at 1:28 am

    Should be pretty simple if understood your problem correctly and you want to validate and sanitize only those three variables.

    function validateAndSanitizeInput(Array $args, Array &$errors) {
    //validation goes in here
    return $args;
    }
    

    In this case the errors array is passed by reference so you’ll be able to get the error messages from it after the function has been called.

    $errors = array();
    $values = validateAndSanitizeInput($_POST, $errors);
    //print $errors if not empty etc.
    

    By the way you could replace “is_null($content) || $content == “”” with “empty($content)”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to refactor code like this: int x=4; int y=x; System.out.println(y); How can
I want to refactor this mumbo jumbo of a method to make it more
I just wrote this piece of code but I'm not quite happy about it.
I have a controller that I want to refactor. This controller basically renders a
I have the following code, I want to refactor the duplication out of: public
I have a bunch of scripts which I want to refactor into modules. This
I want to refactor some C# modules that read data from SQL Server 2008
I want to assign the decimal variable "trans" to the double variable "this.Opacity". decimal
Data binding in ActionScript is really cool. But what if I want to refactor
We have this anonymous function in our code, which is part of the jQuery's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.