Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace words outside double and single quotes

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?

like image 808
Nick Avatar asked Jun 07 '12 09:06

Nick


People also ask

How do you replace single quotes and double quotes?

Use the String. replace() method to replace single with double quotes, e.g. const replaced = str. replace(/'/g, " ); . The replace method will return a new string where all occurrences of single quotes are replaced with double quotes.

Are single and double quotes interchangeable?

The short answer is that it depends on the country that you are writing in. In British and Australian English, one typically uses single quotes. If you're writing in North America, double quote marks are typically used.

How do you replace a double quote in a string?

If you want to add double quotes(") to String, then you can use String's replace() method to replace double quote(") with double quote preceded by backslash(\").

How do you replace a single quote?

Method 1 : Using the replace() method To replace a single quote from the string you will pass the two parameters. The first is the string you want to replace and the other is the string you want to place. In our case it is string. replace(” ' “,” “).


2 Answers

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".'
like image 129
Timm Avatar answered Sep 17 '22 05:09

Timm


I came up with this:

function replaceOutsideDoubleQuotes($search, $replace, $string) {
    $out = '';
    $a = explode('"', $string);
    for ($i = 0; $i < count($a); $i++) {
        if ($i % 2) $out .= $a[$i] . '"';
        else $out .= str_replace($search, $replace, $a[$i]) . '"';
    }
    return substr($out, 0, -1);
}

The logic is: you explode string by double-quotes, so the odd elements of the returning string-array represent text outside quotes, and even ones represents text inside double-quotes.

So, you can build your output by concatenating original parts and replaced parts alternatively, ok?

Working example here: http://codepad.org/rsjvCE8s

like image 39
lorenzo-s Avatar answered Sep 21 '22 05:09

lorenzo-s