Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I adapt my regex to allow for escaped quotes?
    primarykey
    data
    text
    <h2>Introduction</h2> <p>First my general issue is that I want to string replace question marks in a string, but only when they are <em>not</em> quoted. So I found a similar answer on SO (<a href="https://stackoverflow.com/a/10929115/461813">link</a>) and began testing out the code. Unfortunately, of course, the code does not take into account escaped quotes.</p> <p>For example: <code>$string = 'hello="is it me your are looking for\\"?" AND test=?';</code></p> <p>I have adapted a regular expression and code from <a href="https://stackoverflow.com/a/10929115/461813">that answer</a> to the question: <a href="https://stackoverflow.com/questions/10928935/how-to-replace-words-outside-double-and-single-quotes/10929115">How to replace words outside double and single quotes</a>, which is reproduced here for ease of reading my question:</p> <pre><code>&lt;?php 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; } ?&gt; </code></pre> <h2>Actual issue</h2> <p>So I have attempted to adjust the pattern to allow for it to match anything that is not a quote <code>"</code> and quotes that are escaped <code>\"</code>:</p> <pre><code>&lt;?php $pattern = '/("(\\"|[^"])*"' . '|' . "'[^']*')/"; // when parsed/echoed by PHP the pattern evaluates to // /("(\"|[^"])*"|'[^']*')/ ?&gt; </code></pre> <p>But this does not work as I had hoped.</p> <p>My test string is: <code>hello="is it me your are looking for\"?" AND test=?</code></p> <p>And I am getting the following matches:</p> <pre><code>array 0 =&gt; string 'hello=' (length=6) 1 =&gt; string '"is it me your are looking for\"?"' (length=34) 2 =&gt; string '?' (length=1) 3 =&gt; string ' AND test=?' (length=11) </code></pre> <p>Match index two should not be there. That question mark should be considered part of match index 1 only and not repeated separately.</p> <p>Once resolved this same fix should also correct the other side of the main alternation for single quotes/apostrophes as well <code>'</code>.</p> <p>After this is parsed by the complete function it should output:</p> <pre><code>echo str_replace_outside_quotes('?', '%s', 'hello="is it me your are looking for\\"?" AND test=?'); // hello="is it me your are looking for\"?" AND test=%s </code></pre> <p>I hope that this makes sense and I have provided enough information to answer the question. If not I will happily provide whatever you need.</p> <h2>Debug code</h2> <p>My current (complete) code sample is <a href="http://codepad.org/6dpepn2r" rel="nofollow noreferrer">on codepad for forking as well</a>:</p> <pre><code>function str_replace_outside_quotes($replace, $with, $string){ $result = ''; var_dump($string); $pattern = '/("(\\"|[^"])*"' . '|' . "'[^']*')/"; var_dump($pattern); $outside = preg_split($pattern, $string, -1, PREG_SPLIT_DELIM_CAPTURE); var_dump($outside); while ($outside) { $result .= str_replace($replace, $with, array_shift($outside)) . array_shift($outside); } return $result; } echo str_replace_outside_quotes('?', '%s', 'hello="is it me your are looking for\\"?" AND test=?'); </code></pre> <h2>Sample input and expected output</h2> <pre><code>In: hello="is it me your are looking for\\"?" AND test=? AND hello='is it me your are looking for\\'?' AND test=? hello="is it me your are looking for\\"?" AND test=?' AND hello='is it me your are looking for\\'?' AND test=? Out: hello="is it me your are looking for\\"?" AND test=%s AND hello='is it me your are looking for\\'?' AND test=%s hello="is it me your are looking for\\"?" AND test=%s AND hello='is it me your are looking for\\'?' AND test=%s In: my_var = ? AND var_test = "phoned?" AND story = 'he said \'where is it?!?\'' Out: my_var = %s AND var_test = "phoned?" AND story = 'he said \'where is it?!?\'' </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload