Using echo $sql after that to see the string being constructed may help. The first two lines do what the comment says, then the implode/explode business replaces spaces (beween the domains) with '),('so that
domain1 domain2 domain3 domain4
becomes
domain1'),('domain2'),('domain3'),('domain4
which, together with the (' and ') on the adjacent lines, makes for a list of records to insert.
I haven't tested it either - I don't use MySQL so I don't recall if it recognises the syntax - so testing would have to be done by someone who's in a position to try it and see if it works.
I do see one fault with it though in that it might accidentally insert empty domains due to two consecutive spaces appearing in the string. I'd replace the $patterns array with the string
/[ \n,;]+/
so that you could happily have
domain1; domain2;
domain3;domain4
and it will be replaced by
domain1 domain2 domain3 domain4
. And then instead of explode/implode, just
str_replace(' ', "'),('", $domains)
to turn each (single) space into that '),(' glue.