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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:37:39+00:00 2026-05-28T18:37:39+00:00

Environment: PHP 5.3 I’m trying to write my own query parameter substitution method. Basically

  • 0

Environment: PHP 5.3

I’m trying to write my own query parameter substitution method. Basically I want to take this:

select * from xxx where a=? and b>?

and convert it to

select * from xxx where a=1 and b>2

Naturally, assuming that values for all the ? parameters are known. OK, so that’s a bit simplified, but enough for the question.

So, what I need to do, is to find all the ? marks in the given string. Easy, right? But there’s one catch: I don’t want to find the marks that are inside strings or comments. So, in this string:

select *  -- I know * is bad, but just once can't hurt, right?
from xxx /* ? */ where a=? and b='Question?'

Only one of the ? marks should be replaced.

My intuition tells me that PHP’s preg_replace() should be up to the task… but my regex knowledge fails me in constructing an appropriate pattern. 🙁 I could also just parse it “by hand”, but I’m worried that the performance will take an inappropriate hit.

So – can this be done quickly via regexes (and if yes, what would be the pattern), or should I just parse it manually character-by-character?

  • 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-28T18:37:39+00:00Added an answer on May 28, 2026 at 6:37 pm

    This is a difficult problem for regexes. A parser would be more suitable, but as long as certain constraints are met, regexes might just work. The constraints are:

    • no nested comments
    • no escaped quotes inside strings
    • no single quotes inside comments
    • no comment delimiters inside strings
    • all quotes and comments are correctly balanced
    • there is a maximum line length that you can specify

    If those are the case, then you can simply look for a ? that is

    • not preceded (on the same line) by --.
    • not followed by */ unless /* occurs first and
    • followed by an even number of quotes,

    Assuming a maximum line length of 100, this gives you

    $result = preg_replace(
        '%(?<!--.{0,100})    # Assert no -- preceding on this line
        \?                   # Match a ?
        (?!                  # Assert that it\'s impossible to match...
         (?s:                #  (allowing the dot to match newlines here):
          (?!/\*)            #  (and making sure there is no intervening /*)
          .                  #  any character
         )*                  # zero or more times, if that string is followed by
         \*/                 # */
        )                    # End of lookahead
        (?=                  # Assert that it *is* possible to match
         (?:                 # the following regex:
          [^\']*\'[^\']*\'   #  a string containing exactly two quotes
         )*                  #  repeated zero or more times
         [^\']*              # followed by only non-quote characters
         $                   # until the end of the string.
        )                    # End of lookahead.
        %x', 
        'REPLACE', $subject);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use php in console mode and create an environment to test
I want to create an SQL command in a MySQL/PHP environment where the NOT
work environment: php + mysql I want to add an entity from DAO layer
I'm from a php environment and trying to move to Tomcat/jsp. I'm used to
I really want to setup a portable php environment. I would need to use
I'm trying to install a PHP application on my server. But I'm getting this
I am in the midst of setting up the development environment (PHP/MySQL) for my
How do you setup a multi-developer XDebug PHP environment? I have the following setup:
What is the WebORB for PHP runtime environment? It supposedly connects Flex, Flash, AJAX
I am thinking along the lines of replicating a web hosts PHP setup environment

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.