$val) { $d = self::loadXMLValue( $val ); if ($d!==false) $row[$key] = $d; } return $row; } /** * parse XML from string * @param string $val * @return DOMDocument oe false **/ static function loadXMLValue( $val ) { if (is_string($val) && preg_match("/^\s*<.*>\s*$/m",$val)) { // @todo better isXML() needed. This is only a "this may be an XML"... $val = preg_replace("/&([^a])?/", "&\\1", $val); // encode & to & if it's not encoded yet //var_dump($val); $d = new DOMDocument; $d->encoding='UTF-8'; if ($d->loadXML($val, LIBXML_NOXMLDECL)) { return $d; }else{ return false; /* Error parsing XML */ } } return false; /* Probably not an XML */ } /** * @param key needle * @param value new value - will be urlencoded! * @param $query string HTTP query string (without the leading '?') * @param separator Default '&' can be overridden to '&' * @description parse query * @return query string **/ static function addOrReplaceQueryParamValue( $key, $value, $query=NULL, $separator="&" ) { if ($query===NULL) { $query=$_SERVER["QUERY_STRING"]; } //echo "

before: $query

"; $value=urlencode($value); if ($query=="") { $ret="?".$key."=".$value; }else{ $ret = preg_replace("/$key=([^&]*)/", $key."=".$value, $query); if ($ret==$query) { $ret.=$separator.$key."=".$value; } } //echo "

after: $query

"; return $ret; } /** * removes multiplicate keys from a HTTP QUERY STRING. The right side is stronger. * @param string $query HTTP QUERY STRING * @return string HTTP QUERY STRING **/ static function uniqueQuery( $query ) { $parts = explode("&",$query); foreach ($parts as $n=>$part) { list($key, $val)=explode("=", $part); $unique[$key]=$part; } return implode("&", $unique); } /** * validate an email address syntactically * * @param string $val the email address * @return boolean true on success, else false **/ static function isValidEmail ( $val ) { if ($val=="") return false; for ($i=0;$i122 || ($o>90 && $o<97) || ($o>57 && $o<65) || $o<48) { return false; } } } return (boolean)preg_match ('/^[^\(\)<>@,;:\\\"\[\]]+@([a-zA-Z0-9-]+\.)+[a-zA-Z0-9-]+$/',$val); } /** * like array_merge (second overwrites first), but on DOMDocuments and based on a unique XPath (e.g. /columnList/column/@name ) * @fixme now this works only on namedItems ! * @return DOMDocument **/ static function DOMDocument_merge( $DOM1, $DOM2, $mergePath="/" ) { // if (!($DOM1 instanceof DOMDocument)) $DOM1 = new DOMDocument; $xp1 = new DomXpath($DOM1); $n1 = $xp1->query( $mergePath ); $xp2 = new DomXpath($DOM2); $n2 = $xp2->query( $mergePath ); foreach ( $n2 as $node) { $child = $DOM1->importNode($node, TRUE); $name = $child->attributes->getNamedItem("name")->value; $xPathString = $mergePath."[@name='".$name."']"; $existingNodes = $xp1->query( $xPathString ); if ($existingNodes->length > 1) throw Exception("merge target colDOM not unique by $mergePath"); if ($existingNodes->length == 1) { $existingNode = $existingNodes->item(0); $DOM1->documentElement->replaceChild( $child, $existingNode ); //echo "$name replaced"; }else{ $DOM1->documentElement->appendChild( $child ); //echo "$name appended"; } } return $DOM1; } /** * implementation of sh command "rm -rf $f" in php **/ static function rmrf($f) { if (is_dir($f)) { foreach(glob($f.'/*') as $sf) { if (is_dir($sf) && !is_link($sf)) { self::rmrf($sf); } else { unlink($sf); } rmdir($f); } }else{ if (is_file($f)) unlink($f); } } } ?>