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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:35:10+00:00 2026-06-18T10:35:10+00:00

How could I convert something like this: hi (text here) and (other text) come

  • 0

How could I convert something like this:

"hi (text here) and (other text)" come (again)

To this:

"hi \(text here\) and \(other text\)" come (again)

Basically, I want to escape “only” the parentheses that are inside quotes.

EDIT

I’m to new in Regex and so, I tried this:

$params = preg_replace('/(\'[^\(]*)[\(]+/', '$1\\\($2', $string);

But this will just escape the first occurrence of (.

EDIT 2

Maybe I should mention that my string could have these parentheses already escaped and, in this case, I don’t want to escaped them again.

By the way, I need it working for both double and single quotes but I think that I can do that as long as I have a working example for one of them.

  • 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-18T10:35:11+00:00Added an answer on June 18, 2026 at 10:35 am

    This should do it for both single and double quotes:

    $str = '"hi \(text here)" and (other text) come \'(again)\'';
    
    $str = preg_replace_callback('`("|\').*?\1`', function ($matches) {
        return preg_replace('`(?<!\\\)[()]`', '\\\$0', $matches[0]);
    }, $str);
    
    echo $str;
    

    output

    "hi \(text here\)" and (other text) come '\(again\)'
    

    It is for PHP >= 5.3. If you have a lower version (>=5) you have to replace the anonymous function in the callback with a separate function.

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

Sidebar

Related Questions

How could i convert this string 03/16/2012 03:22PMusing php to store in datetime format
How could I convert a VimOutliner file to Markdown? In other words, I how
I would like to ask how could I convert a string which inserted by
Could anyone please help me convert this code to vb.net, I have tried it
Could someone please help me to convert this String into a Map ... String
A while ago I posted this question asking if it's possible to convert text
I have something like this: uses MSXML2_TLB; type TDocumentType = (dtOrder, dtInvoice, dtStatus, dtError);
I've posted something like this a bit ago and it helped out but i
Is it possible to convert this code into one LINQ query. I could have
my first time asking something here. I have a text file of names, 1

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.