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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:15:25+00:00 2026-06-16T04:15:25+00:00

I have a string which is build up like this: [propertyname]=[value] Both the propertyname

  • 0

I have a string which is build up like this:

[propertyname]=[value]

Both the propertyname and the value can be encapsulated by either single or double quotes.

So i could receive a string which looks like this:

"height"='max'

or:

'height'='max'

As long as both the propertyname and value are encapsulated by the same type of quotes.

What i need to do is remove the quotes. But only around the propertyname and value! Because the following could very well be a valid string too:

"blaat"="Some 'random' blaat"

The end result should be:

blaat=Some 'random' blaat

I have the following regex which works. But it only works when i’m either checking for double quotes or single quotes. When i try to combine them with the | operator, then it doesn’t work anymore.

<?php
$string = '"height"=\'something "else" in here\'';

//echo preg_replace ( '#"(.*?)"#', '$1', $string );
//echo preg_replace ( '#\'(.*?)\'#', '$1', $string );
echo preg_replace ( '#("(.*?)"|\'(.*?)\')#', '$1', $string );
?>

So i could simply do two preg_replace calls, but that’s a nasty work around considering regex should be able to handle this in one call…

Any idea what the problem is?

  • 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-16T04:15:27+00:00Added an answer on June 16, 2026 at 4:15 am

    Your regex should be matching correctly, but you have a problem: In your “combined regex”, $1 refers to the entire match (because the first set of parentheses encloses the entire match), so you’re replacing the match with itself, including the quotes.

    ("(.*?)"|\'(.*?)\')
    ^ ^        ^
    |-+--- $1  |---- $3
      |--- $2
    

    Now, you could simply drop the outer parentheses:

    "(.*?)"|\'(.*?)\'
     ^        ^
     |--- $1  |---- $2
    

    But then you have a different problem: You either need to replace the match with $1 or $2, depending on which half of the regex did match. Since you can’t know that in advance, that won’t be easy. You could possibly try and replace with $1$2, but I don’t know whether PHP would allow a backreference to a group that didn’t participate in the match.

    Better play it safe and use a regex that can handle both cases at once, including escaped quotes within the quoted strings:

    $result = preg_replace(
        '/(        # Match and capture (group 1):
         ["\']     # an opening quote character
        )          # (End of group 1).
        (          # Now match and capture (group 2):
         (?:       #  Either...
          \\\\.    #   an escaped character
         |         #  or...
          (?!\1)   #   (as long as it is not the closing quote)
          .        #   any other character.
         )*        #  Repeat as needed.
        )          # (End of group 2)
        \1         # Now match the closing quote./x', 
        '\2', $subject);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to build a map[string]map[string]string which will look something like this: {
I have a string which looks like a hash: { :key_a => { :key_1a
I have an auto-generated file which looks something like this... static void do_SomeFunc1(void* parameter)
In my layout xml file I have several buttons like this which share the
I have a c++ code which build an expiration date string for cookie (something
I have the string like this: String s = word=PS1,p1,p2,p3=q1,q2|word2=PS3,p4,p5,p6=q3; or like this: String
I have a string which is like AB-123,XYZ-456,AB-11,AB-3423,XYZ-1 I want to separate out all
I have a part of code like this Assembly myAssembly = Assembly.GetExecutingAssembly(); string[] names
I have an xml file that looks like this: <args> <sometag value=abc /> <anothertag
I have a string built from a few segments, which are not separated, but

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.