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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:36:45+00:00 2026-06-10T23:36:45+00:00

I’ve been trying to escape all double quotes inside double quotes (yeah, crazy) all

  • 0

I’ve been trying to escape all double quotes inside double quotes (yeah, crazy) all day long and i’m finally giving up. I have this kind of data:

{ "test": "testing with "data" like this", "subject": "trying the "special" chars" }

I’ve been trying to preg_replace every " with \" inside something like this /"(.*)+, "/ which means everything inside double quotes, followed by a comma and space.

I need a way to turn this:

{ "test": "testing with "data" like this", "subject": "trying the "special" chars" }

Into this:

{ "test": "testing with \"data\" like this", "subject": "trying the \"special\" chars" }

Using preg_replace.

  • 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-10T23:36:47+00:00Added an answer on June 10, 2026 at 11:36 pm

    Looking at your regex I would suggest reading up on regex greediness. If you are selecting everything between quotes to the first comma, you will run into problems. The first thing returned would be test": "testing with "data" like this so then if you replaced all " with \" you would have test\": \"testing with \"data\" like this which obviously is not what you want. I would recommend using something like this:

    /"((?:.|\n)*?)"\s*[:,}]\s*/
    

    Explanation

    • "((?:.|\n)*?)" – captures any character between two quotations; the minimum amount while still having the pattern be true
    • \s* – matches 0 or more whitespace characters
    • [:,}] – match either a colon, comma, or right bracket character
    • \s* – matches 0 or more whitespace characters

    Using this regex and your data, the first thing returned is test. The next thing returned would be testing with "data" like this so after replacing you would have testing with \"data\" like this.


    UPDATE

    $test = '{ "test": "testing with "data" like this", "subject": "trying the "special" chars" }';
    $pattern = '/"((?:.|\n)*?)"\s*[:,}]\s*/';
    preg_match_all($pattern, $test, $matches);
    foreach($matches[1] as $match){
        $answers[] = str_replace('"','\\"',$match);
    }
    print_r($answers);
    // Outputs
    // Array ( [0] => test [1] => testing with \"data\" like this [2] => subject [3] => trying the \"special\" chars )
    

    UPDATE 2

    I think using preg_match_all and then str_replace is a better way to solve your problem because that regex is much more stable. But if you insist on using preg_replace then you can use this code:

    $string = '{ "test": "testing with "data" like this", "subject": "trying the "special" chars" }';
    $pattern = '/(?<!:|: )"(?=[^"]*?"(( [^:])|([,}])))/';
    $string = preg_replace($pattern, '\\"', $string);
    print_r($string);
    //Outputs
    //{ "test": "testing with \"data\" like this", "subject": "trying the \"special\" chars" }
    

    Explanation

    • (?<! – starts a negative lookbehind
    • :|: ) – matches a colon or a colon with a space and ends the lookbehind
    • " – matches a quotation
    • (?= – starts a positive lookahead
    • [^"]*? – match anything except a quotation; the minimum amount while still having the pattern be true
    • "(( [^:])|([,}])) – matches a quotation followed by a space and anything but a colon OR it matches a quotation followed by either a comma or right bracket
    • ) – ends the lookahead

    You can read more about regex lookaheads here. I think that this regex is messy although technically it works. I was going to keep playing with it to make it better but I’m tired so I’m going to bed now. This regex allows your data to be more loosely typed. Both of these work, and any combination of them:

    { "test" : "testing with "data" like this" , "subject" : "trying the "special" chars" }
    {"test":"testing with "data" like this","subject":"trying the "special" chars"}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
I have a text area in my form which accepts all possible characters from
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,

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.