DB error handling refactoring.
Rewritten preg_replace() with PREG_REPLACE_EVAL flag (fixed partially by volter9).