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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:43:16+00:00 2026-06-16T09:43:16+00:00

I need a regex for my PHP script that will help me manipulate some

  • 0

I need a regex for my PHP script that will help me manipulate some strings using preg_replace.

For example, say I have the following string:

myvar = first_value AND var2 = second_value

I would like to have it converted to:

myvar = 'first_value' AND var2 = 'second_value'

However, I would also like to have it ignore when there is already a single quote around the value. So myvar = first_value AND var2 = 'second_value' will become myvar = 'first_value' AND var2 = 'second_value' and not myvar = 'first_value' AND var2 = ''second_value''.

I would also like it to deal with spaces within sub-strings when there is already a single quote around the value. So myvar = first_value AND var2 = 'second value' will become myvar = 'first_value' AND var2 = 'second value' and not myvar = 'first_value' AND var2 = 'second' value'.

NOTE: At no time will the string contain variable assignment with unquoted string values that have spaces in them i.e. myvar = first value is a scenario that will never arise. However, the string can contain any other character, even special characters e.g. myvar = &%$@_imspecial_* is totally valid.

The PHP Code I was working with looked something like below (#DontLaugh):

$col_clause_str = "myvar = first_value AND var2 = second_value";
$replace_pattern_str = '/([\w\@\-]+)\s*(\=|\>|\>\=|\<|\<\=)\s*\'{0,1}([\w\@\.\:\+\-\/\|\{\}\[\]\~\%\$\*\!]+)\'{0,1}/i';
$replace_str = '\1 \2 \'\3\'';
$col_clause_str = preg_replace($replace_pattern_str, $replace_str, $col_clause_str);

How can I improve this Regex?!

Thanks in advance.

EDIT:
I updated the question to be more descriptive.

  • 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-16T09:43:17+00:00Added an answer on June 16, 2026 at 9:43 am

    You can replace this regex: –

    "/(?<==\s)(\w+)/i"
    

    with: –

    '\1'
    

    Update: –

    Ok, for your updated question, you can use this regex: –

    "/(?<==\s)([^\s']+)/i"
    

    And replace it with same string – '\1'

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

Sidebar

Related Questions

I need some help with creating a regex for my php script. Basically, I
I need some regex help, please. I have the following regex used in PHP:
I need a PHP Regex that can parse .strings files. In particular, it needs
I need a regex or a function in PHP that will validate a string
I need the regex to find function calls in strings in php, I have
I have a python script I wrote that I need to port to php.
I'm setting up a PHP script that will have emails piped to it from
i need a regex for php that returns the number from strings where starts
i need to find inline javascript with php regex. I can find external script,
I need to build a regex (in PHP) that match if a string has

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.