Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: ldap_explode_dn - Manual
[go: Go Back, main page]

PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<ldap_errorldap_first_attribute" width="11" height="7"/>
view the version of this page
Last updated: Tue, 21 Dec 2004

ldap_explode_dn

(PHP 3, PHP 4 , PHP 5)

ldap_explode_dn -- DN を構成要素毎に分割する

説明

array ldap_explode_dn ( string dn, int with_attrib)

ldap_explode_dn() 関数は、 ldap_get_dn()により返されたDNを分割し複数の要 素に分けるために使用されます。各部分は、相対区分名(Relative Distinguished Name または RDN)と呼ばれます。 ldap_explode_dn() は、これらの全ての要素を含む 配列を返します。 with_attrib は、RDN が値のみを返すのか、 あるいは、属性を同時に返すのかを指定するために使用されます。 属性を有する RDN (すなわち、属性=値 フォーマットで) を得るために はwith_attrib を 0 とし、値のみを得るために は1 にセットします。



add a note add a note User Contributed Notes
ldap_explode_dn
DavidSmith at byu dot net
16-Sep-2003 01:06
[ Editor's Note: The segfault has been fixed and will not occur in PHP 4.3.4 or PHP 5.0.0 when they are released.  However, it is still important to escape special characters as detailed below. ]

If your DN contains < or > characters, you must escape them with a backslash or ldap_explode_dn() will give you a "wrong parameter count" error or even a segmentation fault.

For example, these calls will fail with a "wrong parameter count" or a seg fault:

  ldap_explode_dn( "cn=<bob>,dc=example,dc=com", 0 );
  ldap_explode_dn( 'cn=<bob>,dc=example,dc=com', 0 );

But this will succeed

  ldap_explode_dn( "cn=\<bob\>,dc=example,dc=com", 0 );

Notice also that the < and > are escaped with hex codes as noted above. This function is a nice wrapper that properly formats all DNs and can safely be called with < and > characters, and UTF-8 characters:

function my_explode_dn( $dn, $with_attributes=0 )
{
       $dn = addcslashes( $dn, "<>" );
       $result = ldap_explode_dn( $dn, $with_attributes );
       //translate hex code into ascii again
       foreach( $result as $key => $value )
               $result[$key] = preg_replace("/\\\([0-9A-Fa-f]{2})/e", "''.chr(hexdec('\\1')).''", $value);
       return $result;
}

I am using php 4.3.1. Good luck!
gabriel at hrz dot uni-marburg dot de
05-Aug-2003 06:27
Keep attention on UTF8 encoded DNs. Since openLDAP >=2.1.2
ldap_explode_dn turns unprintable chars (in the ASCII sense, UTF8
encoded) into \<hexcode>.

Example:

$dn="ou=Universität ,c=DE";
var_dump(ldap_explode_dn($dn,0));

//returns

array(3) {
  ["count"]=>
  int(2)
  [0]=>
  string(19) "ou=Universit\C3\A4t"
  [1]=>
  string(4) "c=DE"
}

Unfortunately, PHP don't support the ldap functions ldap_str2dn and
ldap_dn2str, but by means of preg_replace a workaround is possible to
recover the old behaviour of ldap_explode_dn

// workaround
function myldap_explode_dn($dn,$with_attribute){

$result=ldap_explode_dn ($dn, $with_attrib);
 //translate hex code into ascii again
   foreach($result as $key=>$value){
         $result[$key]=preg_replace("/\\\([0-9A-Fa-f]{2})/e", "''.chr(hexdec('\\1')).''", $value);
   }
   return($result);

}
//
//then follows for the example

$dn="ou=Universität ,c=DE";
var_dump(myldap_explode_dn($dn,0));

//returns

array(3) {
  ["count"]=>
  int(2)
  [0]=>
  string(15) "ou=Universität"
  [1]=>
  string(4) "c=DE"
}
bs at muekno dot de
23-Oct-2001 08:02
Copying is much better than typing!!!!
Just modify the constants.
Best wishes (and thanX 4 this helpfull site),
Bernd Schwaegerl
Mueller-Knoche GmbH, Systemhaus fuer EDV-Loesungen

# Example:

$HOST = "Yourhostname";
$USER_DN = "Yourldapuser_dn";
$PWD = "Ldapuserpassword";
$BASE_DN = "o=Your_organisation";
$SEARCH_OBJECT="sn=YOUR_SEARCH_PERSON_OBJECTS_SN";

$ldap_handle=ldap_connect($HOST);
$bind_result=ldap_bind($ldap_handle,$USER_DN,$PWD);

$search_result=ldap_search($ldap_handle,$BASE_DN,$SEARCH_OBJECT);
$result=ldap_get_entries($ldap_handle,$search_result);
$result_array=ldap_get_entries($ldap_handle,$result);
$whole_dn=$result_array[0]["dn"];

$dn_parts=ldap_explode_dn($whole_dn,0);

<ldap_errorldap_first_attribute" width="11" height="7"/>
 Last updated: Tue, 21 Dec 2004
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2005 The PHP Group
All rights reserved.
This mirror generously provided by: HappySize, Inc.
Last updated: Thu Jan 13 06:11:42 2005 JST