0

ok i do have this small helper function

public static function toJSON($arr){
        $json = json_encode($arr);
        return preg_replace_callback('/(?<=:)"function((?:(?!}").)*}"/',
                                    function($string){
                                        return str_replace(array('\"','/','"','n','t'),array('"','/','"','',''),substr($string[0],1,-1));
                                     },$json);
    }

now what i did there was i created a anonymous function for the callback of preg_replace_callback() function, i checked 3 times already if i missed a single parentheses but my IDE(zend studio) seems not to show any unclosed parentheses.

my error is this

preg_replace_callback() Compilation failed: missing ) at offset 30 (X:\xampp\htdocs\aya\protected\xxx\xxxArray.php:180

where i got wrong?

NOTE: the purpose of this small helper function is to let me create anonymous function inside my json output. if you can recommend me a much better script it would be a big help for me..

3
  • dont make things complex. do step by step Commented Jul 19, 2012 at 2:40
  • well hmm... where is the complexity there? Commented Jul 19, 2012 at 2:42
  • get yourself a good syntax highlighting editor with brace matching feature. try notepad2. Commented Jul 19, 2012 at 2:49

2 Answers 2

1

I'm pretty sure the original author meant to do:

'/(?<=:)"function((?:(?!}").)*})"/'
                               ^

If you would want to capture the function body (with the arguments) for the replacing later, however the replace function uses the 0 index (the whole match) later so you can remove the first ( too:

'/(?<=:)"function(?:(?!}").)*}"/'
                 ^
                 +-- a "(" deleted

This will work too (eats a little less memory as well).

Sign up to request clarification or add additional context in comments.

Comments

1

There is an unclosed parenthesis in

'/(?<=:)"function((?:(?!}").)*}"/'

The editor won't spot it as it won't validate content in strings.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.