[ create a new paste ] login | about

Link: http://codepad.org/6dpepn2r    [ raw code | output | fork ]

PHP, pasted on Nov 13:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
    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=?');


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
string(51) "hello="is it me your are looking for\"?" AND test=?"
string(24) "/("(\"|[^"])*"|'[^']*')/"
array(4) {
  [0]=>
  string(6) "hello="
  [1]=>
  string(34) ""is it me your are looking for\"?""
  [2]=>
  string(1) "?"
  [3]=>
  string(11) " AND test=?"
}
hello="is it me your are looking for\"?"%s AND test=?


Create a new paste based on this one


Comments: