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

  • Home
  • SEARCH
  • 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 8894951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:38:34+00:00 2026-06-14T23:38:34+00:00

I want to strip user input of SQL executable statements, but the way the

  • 0

I want to strip user input of SQL executable statements, but the way the codebase is I don’t want to establish a DB connection which mysqli_real_escape_string(connection, string) requires.

What is an elegant regex/replace one-liner to sanitize the strings?

<?php
$naughty = array("\\", "{", "}", ")", "(", "[", "]", "\"", ";", ":", ">", "&");
$sanitized = str_replace($naughty, " ", $input_string);
?> 

—UPDATE—-
Example solution. Thanks for the input guys.

<?PHP 
      $testsql = "';\"\\ bork bork bork %A 0x33\n";
      echo $testsql;
     /* $translate = array("0x" => "[HEX]",  ";" => "[SEMICOLON]", "'" => "[QUOTE]", "%" => "[PERCENT]","\"" => "[DOUBLEQUOTE]",
      "\\" => "[BACKSLASH]" ); */
      $translate = array("0x" => " ",  ";" => " ", "'" => " ", "%" => " ","\"" => " ", "\\" => " ");
      echo "<br>";
      $testsql=strtr($testsql, $translate);
      echo $testsql;
?>
  • 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-14T23:38:36+00:00Added an answer on June 14, 2026 at 11:38 pm

    While I definitely agree with the comments and answer that using prepared statements, and not doing this yourself is both smarter, better in many ways and always recommended when you can use them. While you may not have a security problem now, one may suddenly appear in the future; and the risk with prepared statements is just much, much lower. If you can find a workaround for it, do it!

    I feel like I have to give that disclaimer, in order to be taken seriously here.

    Because, I also believe it’s definitely possible to do all the escaping manually; given that the environment is controlled. This is no different than escaping for other formats, such as javascript. Sometimes this is needed. I, for one, once had a need to create a SQL export script with no database available.

    To do this correctly, the following must be assumed:

    • The input string is UTF-8. You validated this.
    • When you execute the mysql string later down the road, the full sql statement is in UTF-8, and so is the connection.
    • No crazy stuff is going on with php’s mbstring function overloading.
    • You don’t trust my untested code, and verify what I’m saying 😉

    Why is UTF-8 so important? Read this article

    Given those two points, to emulate mysql_real_escape_string, you must escape the following characters:

    NUL (ASCII 0), \n, \r, \, ', ", and Control-Z. 
    

    Source: php.net

    To do this, this should be sufficient:

    $translate = array(
      "\x00" => '\\0',
      "\n"   => '\\n',
      "\r"   => '\\r',
      "'"    => '\\\'',
      "\""   => '\\"',
      "\x1a" => '\\Z',
    );
    
    $output = "'" . strtr($input, $translate) . "'";
    

    As you can see, I did still make sure that my output was surrounded by quotes. Without that, this escaping technique (nor mysqli::real_escape_string) would not be sufficient.

    Alternative techniques I’ve seen:

    • Splitting up every byte, and surrounding it with MySQL’s CHAR function.
    • Base64-encoding the string in PHP, and base64-decoding it with MySQL 5.6’s FROM_BASE64 function.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to strip all HTML tags and attributes from a user input except
I want to scan a folder whose path is defined by user input &
When receiving user input on forms I want to detect whether fields like username
I have a code blog in which I have a user input form for
In my webpage, I want the website to greet the user, but the username
Basically I want to strip the document of words between blockquotes. I'm a regular
I want to be able to strip off a header from a wav file.
I want to modify an internal webpage to strip away some of the onclick
I want to make a scrip through which i can grab the visitors cookis
want to open pdf file when a user clicks on hyperlink shown in gridview

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.