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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:25:37+00:00 2026-06-05T06:25:37+00:00

For a custom script parser in PHP I would like to replace some words

  • 0

For a custom script parser in PHP I would like to replace some words in a multiline string that contain double and single quotes.
However, only the text that is outside the quotes can be replaced.

Many apples are falling from the trees.    
"There's another apple over there!"    
'Seedling apples are an example of "extreme heterozygotes".'

For example, I would like to replace ‘apple’ with ‘pear’, but only outside the quote sentences. So in this case only ‘apple’ inside ‘Many apples are falling from the trees’ would be targeted.

The above would give the following output:

Many pears are falling from the trees.    
"There's another apple over there!"    
'Seedling apples are an example of "extreme heterozygotes".'

How can I achieve this?

  • 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-05T06:25:39+00:00Added an answer on June 5, 2026 at 6:25 am

    This function does the trick:

    function str_replace_outside_quotes($replace,$with,$string){
        $result = "";
        $outside = preg_split('/("[^"]*"|\'[^\']*\')/',$string,-1,PREG_SPLIT_DELIM_CAPTURE);
        while ($outside)
            $result .= str_replace($replace,$with,array_shift($outside)).array_shift($outside);
        return $result;
    }
    

    How it works It splits by quoted strings but includes these quoted strings, this gives you alternating non-quoted, quoted, non-quoted, quoted etc strings in an array (some of the non-quoted strings may be blank). It then alternates between replacing the word and not replacing, so only non-quoted strings are replaced.

    With your example

    $text = "Many apples are falling from the trees.    
            \"There's another apple over there!\"    
            'Seedling apples are an example of \"extreme heterozygotes\".'";
    $replace = "apples";
    $with = "pears";
    echo str_replace_outside_quotes($replace,$with,$text);
    

    Output

    Many pears are falling from the trees.    
    "There's another apple over there!"    
    'Seedling apples are an example of "extreme heterozygotes".'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to parse JSON string to some my custom object in Action script
I have a custom script that takes hostnames as parameters. I know that I
I have a custom script step in my build process that zips the executable.
I am trying to write a custom script that will keep a list of
i'm trying to create nodes and taxonomy terms through a custom php script by
I've got a custom javascript autocomplete script that hits the server with multiple asynchronous
I have a Unix bash function that executes a script that parses custom environment
I have a script in Filemaker that calls a Brian Dunning hosted custom function
I have a custom script that outputs a list of particular products in csv
I was refactoring some old code of a simple script file parser when I

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.