diff --git a/lib/plugins/authldap/auth.php b/lib/plugins/authldap/auth.php
index 247a0fec20328438d2319a0ab684edd5da92e91b..90ec770e9dc0d50e65155f0f61dd32a7964d1b8d 100644
--- a/lib/plugins/authldap/auth.php
+++ b/lib/plugins/authldap/auth.php
@@ -463,9 +463,12 @@ class auth_plugin_authldap extends DokuWiki_Auth_Plugin {
      * @return string
      */
     protected function _filterEscape($string) {
-        return preg_replace(
-            '/([\x00-\x1F\*\(\)\\\\])/e',
-            '"\\\\\".join("",unpack("H2","$1"))',
+        // see https://github.com/adldap/adLDAP/issues/22
+        return preg_replace_callback(
+            '/([\x00-\x1F\*\(\)\\\\])/',
+            function ($matches) {
+                return "\\".join("", unpack("H2", $matches[1]));
+            },
             $string
         );
     }
@@ -479,6 +482,10 @@ class auth_plugin_authldap extends DokuWiki_Auth_Plugin {
     protected function _openLDAP() {
         if($this->con) return true; // connection already established
 
+        if($this->getConf('debug')) {
+            ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 7);
+        }
+
         $this->bound = 0;
 
         $port    = $this->getConf('port');
@@ -555,6 +562,7 @@ class auth_plugin_authldap extends DokuWiki_Auth_Plugin {
 
         if(!$bound) {
             msg("LDAP: couldn't connect to LDAP server", -1);
+            $this->_debug(ldap_error($this->con), 0, __LINE__, __FILE__);
             return false;
         }