source: branches/2.1/include/functions_xml.inc.php @ 16378

Last change on this file since 16378 was 6364, checked in by plg, 14 years ago

remove all svn:mergeinfo properties

  • Property svn:eol-style set to LF
File size: 4.6 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2010 Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23define( 'ATT_REG', '\w+' );
24define( 'VAL_REG', '[^"]*' );
25
26//------------------------------------------------------------------- functions
27// getContent returns the content of a tag
28//
29// example : getContent( "<name>Joe</name>" ) returns "Joe"
30//
31// It also works with strings containing themself sub-tags :
32// <perso><name>Jean</name><firstname>Billie</fisrtname></perso> ->
33// <name>Jean</name><firstname>Billie</firstname>
34function getContent( $element )
35{
36  // deleting start of the tag
37  $content = preg_replace( '/^<[^>]+>/', '', $element );
38  // deleting end of the tag
39  $content = preg_replace( '/<\/[^>]+>$/', '', $content );
40  // replacing multiple instance of space character
41  $content = preg_replace( '/\s+/', ' ', $content );
42
43  return $content;
44}
45
46// The function get Attribute returns the value corresponding to the
47// attribute $attribute for the tag $element.
48function getAttribute( $element, $attribute )
49{
50//  echo htmlentities($element).'<br /><br />';
51  $regex = '/^<\w+[^>]*\b'.$attribute.'\s*=\s*"('.VAL_REG.')"/i';
52  if ( preg_match( $regex, $element, $out ) ) 
53  {
54    return html_entity_decode($out[1], ENT_QUOTES);
55  }
56  else return '';
57}
58
59// The function encode Attribute returns the xml attribute $attribute="$value"
60function encodeAttribute( $attribute, $value )
61{
62  return $attribute.'="'.htmlspecialchars($value, ENT_QUOTES).'" ';
63}
64
65// The function getChild returns the first child
66// exemple : getChild( "<table><tr>XXX</tr><tr>YYY</tr></table>", "tr" )
67//           returns "<tr>XXX</tr>"
68function getChild( $document, $node )
69{
70  $regex = '/<'.$node.'(\s+'.ATT_REG.'="'.VAL_REG.'")*';
71  $regex.= '(\s*\/>|>.*<\/'.$node.'>)/U';
72
73  if
74    (
75      preg_match( $regex, $document, $out )
76      or
77      preg_last_error() == PREG_NO_ERROR
78    )
79  {
80    return $out[0];
81  }
82  else
83  {
84    die('getChild: error ['.preg_last_error().'] with preg_match function');
85  }
86}
87
88// getChildren returns a list of the children identified by the $node
89// example :
90//     getChild( "<table><tr>XXX</tr><tr>YYY</tr></table>", "tr" )
91//     returns an array with :
92//          $array[0] equals "<tr>XXX</tr>"
93//          $array[1] equals "<tr>YYY</tr>"
94function getChildren( $document, $node )
95{
96  $regex = '/<'.$node.'(\s+'.ATT_REG.'="'.VAL_REG.'")*';
97  $regex.= '(\s*\/>|>.*<\/'.$node.'>)/U';
98
99  if
100    (
101      preg_match_all( $regex, $document, $out )
102      or
103      preg_last_error() == PREG_NO_ERROR
104    )
105  {
106    return $out[0];
107  }
108  else
109  {
110    die('getChild: error ['.preg_last_error().'] with preg_match_all function');
111  }
112}
113
114// get_CodeXML places the content of a text file in a PHP variable and
115// return it. If the file can't be opened, returns false.
116function getXmlCode( $filename )
117{
118  if (function_exists('ini_set'))
119  {
120    // limit must be growed with php5 and "big" listing file
121    ini_set("pcre.backtrack_limit", pow(2, 32));
122  }
123
124  if (!fetchRemote($filename, $xml_content))
125  {
126    return false;
127  }
128
129  $xml_content = str_replace( "\n", '', $xml_content );
130  $xml_content = str_replace( "\t", '', $xml_content );
131
132  return $xml_content;
133}
134?>
Note: See TracBrowser for help on using the repository browser.