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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:39:55+00:00 2026-05-15T02:39:55+00:00

Look at the following string : SELECT column1 , column2, column3 FROM table1 WHERE

  • 0

Look at the following string:

SELECT
    column1 ,
    column2, column3
FROM
    table1
WHERE
    column1 = 'text, "FROM" \'from\\\' x' AND
    column2 = "sample text 'where' \"where\\\" " AND
    ( column3 = 5 )

I need to escape unnecessary white space characters from the string like:

  • removing white space from beginning and ending position of , ( ) etc
  • removing newline (\r\n) and tabs (\t)

But one thing. The remove process could not remove white spaces from the quoted string like:

  • ‘text, “FROM” \’from\\’ x’
  • “sample text ‘where’ \”where\\” “

etc.

i need to use the PHP function: preg_replace($pattern, $replacement, $string);

So what will be the value of $pattern and $replacement where the value of $string is the given SQL.

  • 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-15T02:39:56+00:00Added an answer on May 15, 2026 at 2:39 am

    A single regex-pattern and replacement string string will not work. Your first step could be to tokenize the input string: try first to match comments and string literals, and then try to match white space chars and lastly non-space chars. A quick demo:

    $text = <<<BLOCK
    SELECT
        column1 ,
        column2, column3
    FROM
        table1
    -- a comment line ' " ...
    WHERE
        column1 = 'text, "FROM" \\'from\\\\\\' x' AND
        column2 = "sample text 'where' \\"where\\\\\\" " AND
        ( column3 = 5 )
    BLOCK;
    
    echo $text . "\n\n";
    
    preg_match_all('/
        --[^\r\n]*                # a comment line
        |                         # OR
        \'(?:\\\\.|[^\'\\\\])*\'  # a single quoted string
        |                         # OR
        "(?:\\\\.|[^"\\\\])*"     # a double quoted string
        |                         # OR
        `[^`]*`                   # a string surrounded by backticks
        |                         # OR
        \s+                       # one or more space chars
        |                         # OR
        \S+                       # one or more non-space chars
    /x', $text, $matches);
    
    print_r($matches);
    

    produces:

    SELECT
        column1 ,
        column2, column3
    FROM
        table1
    -- a comment line ' " ...
    WHERE
        column1 = 'text, "FROM" \'from\\\' x' AND
        column2 = "sample text 'where' \"where\\\" " AND
        ( column3 = 5 )
    
    Array
    (
        [0] => Array
            (
                [0] => SELECT
                [1] => 
    
                [2] => column1
                [3] =>  
                [4] => ,
                [5] => 
    
                [6] => column2,
                [7] =>  
                [8] => column3
                [9] => 
    
                [10] => FROM
                [11] => 
    
                [12] => table1
                [13] => 
    
                [14] => -- a comment line ' " ...
                [15] => 
    
                [16] => WHERE
                [17] => 
    
                [18] => column1
                [19] =>  
                [20] => =
                [21] =>  
                [22] => 'text, "FROM" \'from\\\' x'
                [23] =>  
                [24] => AND
                [25] => 
    
                [26] => column2
                [27] =>  
                [28] => =
                [29] =>  
                [30] => "sample text 'where' \"where\\\" "
                [31] =>  
                [32] => AND
                [33] => 
    
                [34] => (
                [35] =>  
                [36] => column3
                [37] =>  
                [38] => =
                [39] =>  
                [40] => 5
                [41] =>  
                [42] => )
            )
    
    )
    

    and then you can iterate over your tokenized $matches array and replace the space-matches where you see fit.

    But as you might have read in my already deleted comment, a better option would be to use some dedicated SQL parser to perform this tokenizing: I am not fluent in SQL, but I am fairly sure my demo above can be easily broken.

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

Sidebar

Related Questions

Please look the following query: SELECT ID, START, END FROM TABLEA the result is:
I have the following string: \\\\AAA.AA.A.AA\\d$\\ivr\\vm\\2012May\\29\\10231_1723221348.vox I would like it to look like: \\AAA.AA.A.AA\d$\ivr\vm\2012May\29\10231_1723221348.vox
Pls look at following code <select name=VideoType id=VideoType style=width:60px> <option value=All>All</option> <option value=Movie>Movie</option> <option
I have a query that looks like the following: SELECT * FROM table WHERE
I am following this tutorial var groups = from sharePointGroup in groupsXml.Root.Elements(Group) select new
I have a Recordset obtained by the following query: SELECT DISTINCT [Number] FROM NUMBERS
Have a look at following scenario: public class ParentClass { private Integer testVar =
I have HttpClient 4.1. Please have a look at following program: import org.apache.http.client.methods.*; import
I am doing image Read/Copy operations in WPF application. Please look at following piece
I have some difficulties with understanding BindingSource's behaviour. Let's look at following example: Creating

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.