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

Last change on this file since 13 was 13, checked in by z0rglub, 21 years ago

* empty log message *

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.1 KB
Line 
1<?php
2/***************************************************************************
3 *                           functions_xml.inc.php                         *
4 *                            -------------------                          *
5 *   application          : PhpWebGallery 1.3                              *
6 *   author               : Pierrick LE GALL <pierrick@z0rglub.com>        *
7 *                                                                         *
8 ***************************************************************************
9
10 ***************************************************************************
11 *                                                                         *
12 *   This program is free software; you can redistribute it and/or modify  *
13 *   it under the terms of the GNU General Public License as published by  *
14 *   the Free Software Foundation;                                         *
15 *                                                                         *
16 ***************************************************************************/
17
18//------------------------------------------------------------------ constantes
19define( ATT_REG, '\w+' );
20define( VAL_REG, '[^"]*' );
21
22//------------------------------------------------------------------- functions
23// getContent returns the content of a tag
24//
25// example : getContent( "<name>Joe</name>" ) returns "Joe"
26//
27// It also works with strings containing themself sub-tags :
28// <perso><name>Jean</name><firstname>Billie</fisrtname></perso> ->
29// <name>Jean</name><firstname>Billie</firstname>
30function getContent( $element )
31{
32  // deleting start of the tag
33  $content = preg_replace( '/^<[^>]+>/', '', $element );
34  // deleting end of the tag
35  $content = preg_replace( '/<\/\w+>$/', '', $content );
36  // replacing multiple instance of space character
37  $content = preg_replace( '/\s+/', ' ', $content );
38
39  return $content;
40}
41
42// The function get Attribute returns the value corresponding to the attribute
43// $attribute for the tag $element.
44function getAttribute( $element, $attribute )
45{
46  $regex = '/^<\w+[^>]*'.$attribute.'\s*=\s*"('.VAL_REG.')"/i';
47  if ( preg_match( $regex, $element, $out ) ) return $out[1];
48  else return '';
49}
50
51function deprecated_getAttribute( $element, $attribute )
52{
53  // Retrieving string with tag name and all attributes
54  $regex = '/^<\w+( '.ATT_REG.'="'.VAL_REG.'")*/';
55  preg_match( $regex, $element, $out );
56
57  // Splitting string for retrieving separately attributes
58  // and corresponding values
59  $regex = '/('.ATT_REG.')="('.VAL_REG.')"/';
60  preg_match_all( $regex, $out[0], $out );
61
62  // Searching and returning the value of the requested attribute
63  for ( $i = 0; $i < sizeof( $out[0] ); $i++ )
64  {
65    if ( $out[1][$i] == $attribute )
66    {
67      return $out[2][$i];
68    }
69  }
70  return '';
71}
72       
73// The function getChild returns the first child
74// exemple : getChild( "<table><tr>XXX</tr><tr>YYY</tr></table>", "tr" )
75//           returns "<tr>XXX</tr>"
76function getChild( $document, $node )
77{
78  $regex = '/<'.$node.'(\s+'.ATT_REG.'="'.VAL_REG.'")*';
79  $regex.= '(\s*\/>|>.*<\/'.$node.'>)/U';
80
81  preg_match( $regex, $document, $out );
82  return $out[0];
83}
84
85// getChildren returns a list of the children identified by the $node
86// example :
87//     getChild( "<table><tr>XXX</tr><tr>YYY</tr></table>", "tr" )
88//     returns an array with :
89//          $array[0] equals "<tr>XXX</tr>"
90//          $array[1] equals "<tr>YYY</tr>"
91function getChildren( $document, $node )
92{
93  $regex = '/<'.$node.'(\s+'.ATT_REG.'="'.VAL_REG.'")*';
94  $regex.= '(\s*\/>|>.*<\/'.$node.'>)/U';
95
96  preg_match_all( $regex, $document, $out );
97  return $out[0];
98}
99       
100// get_CodeXML places the content of a text file in a PHP variable and
101// return it. If the file can't be opened, returns false.
102function getXmlCode( $filename )
103{
104  $file = fopen( $filename, 'r' );
105  if ( !$file )
106  {
107    return false;
108  }
109  while ( !feof( $file ) )
110  {
111    $xml_content .= fgets( $file, 1024 );
112  }
113  fclose( $file );
114  $xml_content = str_replace( "\n", '', $xml_content );
115  $xml_content = str_replace( "\t", '', $xml_content );
116
117  return $xml_content;
118}
119?>
Note: See TracBrowser for help on using the repository browser.