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 7725245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:53:25+00:00 2026-06-01T04:53:25+00:00

Is there a native PHP function for rejecting invalid $_POST data from simple text

  • 0

Is there a native PHP function for rejecting invalid $_POST data from simple text fields?

Currently I’m using custom filter functions with regular expressions, and would really like to simplify my code if possible.

For example, let’s say I have a form field for entering a last name. I want to check the submitted value and immediately reject the input if it contains any invalid characters.

Here are my criteria for considering PHP functions:

  • A person’s last name might be “Smith-Johnson”, “Van Buren”, “O’Malley”, etc., so the function needs to tolerate spaces, hyphens, apostrophes, etc.

  • My goal is to test the input and reject it right away — as opposed to sanitizing it and running it through additional filters and processing steps.

  • Two examples of input that I want to catch are:

    username' --
    username; DELETE FROM users;
    

Here are the options I’m aware of and why I have doubts about using them…

  1. is_string(): This function doesn’t seem to catch either of the two examples above

  2. addslashes(): This function fails the second criterion

  3. htmlentities(), htmlspecialchars(): Same as addslashes(); if input is invalid, I don’t want to keep working with it, even if it’s sanitized

  4. strip_tags(): Fails the second criterion, but preferred over #3 because it eliminates unwanted characters rather than sanitizing them

  5. filter_input(…FILTER_SANITIZE_STRING): As far as I can tell, this is identical to strip_tags(). **

  6. ctype_alpha(), ctype_alnum(): Fails the first criteria because it doesn’t allow spaces and other characters.

  7. Regex/Custom Functions: This is what I’ve been using. I’d really prefer to simplify my code and eliminate the (albeit minimal) regex overhead.

—

** Just curious: does PECL make filter_input() faster than strip_tags()?

  • 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-06-01T04:53:26+00:00Added an answer on June 1, 2026 at 4:53 am

    There is no such function. You’re asking basically how to tell one string from another one while they’re both strings with complex criteria. That’s quite a vague task for a “native” function.

    The most “native” way of achieving that is to use regular expressions, and that’s what you’re already doing.

    It is also against the ideology as $_POST and $_GET are supposed to represent parameters in their raw form, not after passing some validation.

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

Sidebar

Related Questions

Is there a native PHP function that can remove a set of keys from
I just wondered if there were an native PHP function that would replicate the
Is there any native PHP function which would allow me to do this? $number
Is there a native PHP function which tells me how many decimal places a
Is there a native Emacs Lisp function that behaves like strpos() in PHP ?
I was wondering if there is a native C++ (or STL/Boost) function which will
I was wondering if there is a python cognate to PHP's crypt() function that
I am trying to send JSON data from a form using the XMLHttpRequest object.
I have multiple calls to many RESTful services. I translate to PHP using native
Is there any sort of assign-if-not-empty-otherwise-assign-null function in PHP? I'm looking for a cleaner

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.