From 77fd1f51a3c5f5a52f72ef8a299fe368228e2285 Mon Sep 17 00:00:00 2001 From: vdigital Date: Fri, 23 May 2008 21:05:41 +0000 Subject: git-svn-id: http://piwigo.org/svn/trunk@2357 68402e56-0260-453c-a942-63ccdbb3a9ee --- BSF/include/functions_xml.inc.php | 141 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 BSF/include/functions_xml.inc.php (limited to 'BSF/include/functions_xml.inc.php') diff --git a/BSF/include/functions_xml.inc.php b/BSF/include/functions_xml.inc.php new file mode 100644 index 000000000..1a849d434 --- /dev/null +++ b/BSF/include/functions_xml.inc.php @@ -0,0 +1,141 @@ +Joe" ) returns "Joe" +// +// It also works with strings containing themself sub-tags : +// JeanBillie -> +// JeanBillie +function getContent( $element ) +{ + // deleting start of the tag + $content = preg_replace( '/^<[^>]+>/', '', $element ); + // deleting end of the tag + $content = preg_replace( '/<\/[^>]+>$/', '', $content ); + // replacing multiple instance of space character + $content = preg_replace( '/\s+/', ' ', $content ); + + return $content; +} + +// The function get Attribute returns the value corresponding to the +// attribute $attribute for the tag $element. +function getAttribute( $element, $attribute ) +{ +// echo htmlentities($element).'

'; + $regex = '/^<\w+[^>]*\b'.$attribute.'\s*=\s*"('.VAL_REG.')"/i'; + if ( preg_match( $regex, $element, $out ) ) + { + return html_entity_decode($out[1], ENT_QUOTES); + } + else return ''; +} + +// The function encode Attribute returns the xml attribute $attribute="$value" +function encodeAttribute( $attribute, $value ) +{ + return $attribute.'="'.htmlspecialchars($value, ENT_QUOTES).'" '; +} + +// The function getChild returns the first child +// exemple : getChild( "XXXYYY
", "tr" ) +// returns "XXX" +function getChild( $document, $node ) +{ + $regex = '/<'.$node.'(\s+'.ATT_REG.'="'.VAL_REG.'")*'; + $regex.= '(\s*\/>|>.*<\/'.$node.'>)/U'; + + if + ( + preg_match( $regex, $document, $out ) + or + preg_last_error() == PREG_NO_ERROR + ) + { + return $out[0]; + } + else + { + die('getChild: error ['.preg_last_error().'] with preg_match function'); + } +} + +// getChildren returns a list of the children identified by the $node +// example : +// getChild( "XXXYYY
", "tr" ) +// returns an array with : +// $array[0] equals "XXX" +// $array[1] equals "YYY" +function getChildren( $document, $node ) +{ + $regex = '/<'.$node.'(\s+'.ATT_REG.'="'.VAL_REG.'")*'; + $regex.= '(\s*\/>|>.*<\/'.$node.'>)/U'; + + if + ( + preg_match_all( $regex, $document, $out ) + or + preg_last_error() == PREG_NO_ERROR + ) + { + return $out[0]; + } + else + { + die('getChild: error ['.preg_last_error().'] with preg_match_all function'); + } +} + +// get_CodeXML places the content of a text file in a PHP variable and +// return it. If the file can't be opened, returns false. +function getXmlCode( $filename ) +{ + if (function_exists('ini_set')) + { + // limit must be growed with php5 and "big" listing file + ini_set("pcre.backtrack_limit", pow(2, 32)); + } + + $file = fopen( $filename, 'r' ); + if ( !$file ) + { + return false; + } + + $xml_content = ''; + while ( !feof( $file ) ) + { + $xml_content .= fgets( $file, 1024 ); + } + fclose( $file ); + $xml_content = str_replace( "\n", '', $xml_content ); + $xml_content = str_replace( "\t", '', $xml_content ); + + return $xml_content; +} +?> -- cgit v1.2.3