cvs: pear /Net_LDAP/LDAP Entry.php
| From: | Benedikt Hallinger | Date: | Tue, 27 Feb 2007 09:12:36 +0000 |
| Subject: | cvs: pear /Net_LDAP/LDAP Entry.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-44893@lists.php.net to get a copy of this message | ||
beni Tue Feb 27 09:12:36 2007 UTC
Modified files:
/pear/Net_LDAP/LDAP Entry.php
Log:
* Implemented $entry->preg_match() to test single- and multivlalued attribute
http://cvs.php.net/viewvc.cgi/pear/Net_LDAP/LDAP/Entry.php?r1=1.31&r2=1.32&diff_format=u
Index: pear/Net_LDAP/LDAP/Entry.php
diff -u pear/Net_LDAP/LDAP/Entry.php:1.31 pear/Net_LDAP/LDAP/Entry.php:1.32
--- pear/Net_LDAP/LDAP/Entry.php:1.31 Mon Feb 26 11:57:32 2007
+++ pear/Net_LDAP/LDAP/Entry.php Tue Feb 27 09:12:36 2007
@@ -22,7 +22,7 @@
// | Authors: Jan Wagner, Tarjej Huse |
// +--------------------------------------------------------------------------+
//
-// $Id: Entry.php,v 1.31 2007/02/26 11:57:32 beni Exp $
+// $Id: Entry.php,v 1.32 2007/02/27 09:12:36 beni Exp $
require_once("PEAR.php");
require_once('Util.php');
@@ -36,7 +36,7 @@
* @package Net_LDAP
* @author Jan Wagner <wagner@netsols.de>
* @author Tarjej Huse
- * @version $Revision: 1.31 $
+ * @version $Revision: 1.32 $
*/
class Net_LDAP_Entry extends PEAR
@@ -684,5 +684,68 @@
return $this->_ldap;
}
}
+
+ /**
+ * Applies a regular expression onto a single- or multivalued attribute (like preg_match())
+ *
+ * This method behaves like PHPs preg_match() but with some exceptions.
+ * If you want to retrieve match information, then you MUST pass the
+ * $matches parameter via reference! otherwise you will get no matches.
+ * Since it is possible to have multi valued attributes the $matches
+ * array will have a additionally numerical dimension (one for each value):
+ * <code>
+ * $matches = array(
+ * 0 => array (usual preg_match() returnarray),
+ * 1 => array (usual preg_match() returnarray)
+ * )
+ * </code>
+ * Please note, that $matches will be initialized to an empty array inside.
+ *
+ * Usage example:
+ * <code>
+ * $result = $entry->preg_match('/089(\d+)/', 'telephoneNumber',
&$matches);
+ * if ( $result === true ){
+ * echo "First match: ".$matches[0][1]; // Match of value 1, content of first
bracket
+ * } else {
+ * if ( Net_LDAP::isError($result) ) {
+ * echo "Error: ".$result->getMessage();
+ * } else {
+ * echo "No match found.";
+ * }
+ * }
+ * </code>
+ *
+ * Please note that it is important to test for an Net_LDAP_Error, because objects are
+ * evaluating to true by default, thus if a error occured, and you only check using
"==" then
+ * you get misleading results. Use the "identical" (===) operator to test for matches
to
+ * avoid this as shown above.
+ *
+ * @param string $regex The regular expression
+ * @param string $attr_name The attribute to search in
+ * @param array $matches (optional, PASS BY REFERENCE!) Array to store matches in
+ * @return boolean|Net_LDAP_Error TRUE, if we had a match in one of the values, otherwise
false. Net_LDAP_Error in case something went wrong
+ */
+ function preg_match($regex, $attr_name, $matches = array()){
+ $matches = array();
+
+ // fetch attribute values
+ $attr = $this->getValue($attr_name, 'all');
+ if (Net_LDAP::isError($attr)) {
+ return $attr;
+ } else {
+ unset($attr['count']);
+ }
+
+ // perform preg_match() on all values
+ $match = false;
+ foreach ($attr as $thisvalue) {
+ $matches_int = array();
+ if (preg_match($regex, $thisvalue, $matches_int)) {
+ $match = true;
+ array_push($matches, $matches_int); // store matches in reference
+ }
+ }
+ return $match;
+ }
}
?>
\ No newline at end of file