PHP ldap_add function to escape ldap special characters in DN syntax
I'm trying to add some users to my Ldap DB but I get some errors (invalid dn syntax) when I use some special characters like ",.". I need a function that escape all characters. I try preg_quote but I get some errors in some cases. Thanks in advance Code: $user = 'Test , Name S.L'; if(!(ldap_add($ds, "cn=" . $user . ",".LDAP_DN_BASE, $info))) { include 'error_new_account.php'; } DaveRandom EDIT Jan 2013: added support for escaping leading/trailing spaces in DN strings, per RFC 4514 . Thanks to Eugenio for pointing out this issue. EDIT 2014: I added this function to PHP 5.6 . The code below is