source: trunk/include/functions_xml.inc.php @ 637

Last change on this file since 637 was 593, checked in by z0rglub, 20 years ago

update headers to comply with GPL

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