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

Last change on this file since 27 was 21, 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//------------------------------------------------------------------- functions
22// getContent returns the content of a tag
23//
24// example : getContent( "<name>Joe</name>" ) returns "Joe"
25//
26// It also works with strings containing themself sub-tags :
27// <perso><name>Jean</name><firstname>Billie</fisrtname></perso> ->
28// <name>Jean</name><firstname>Billie</firstname>
29function getContent( $element )
30{
31  // deleting start of the tag
32  $content = preg_replace( '/^<[^>]+>/', '', $element );
33  // deleting end of the tag
34  $content = preg_replace( '/<\/[^>]+>$/', '', $content );
35  // replacing multiple instance of space character
36  $content = preg_replace( '/\s+/', ' ', $content );
37
38  return $content;
39}
40
41// The function get Attribute returns the value corresponding to the
42// attribute $attribute for the tag $element.
43function getAttribute( $element, $attribute )
44{
45  $regex = '/^<\w+[^>]*'.$attribute.'\s*=\s*"('.VAL_REG.')"/i';
46  if ( preg_match( $regex, $element, $out ) ) return $out[1];
47  else return '';
48}
49
50function deprecated_getAttribute( $element, $attribute )
51{
52  // Retrieving string with tag name and all attributes
53  $regex = '/^<\w+( '.ATT_REG.'="'.VAL_REG.'")*/';
54  preg_match( $regex, $element, $out );
55
56  // Splitting string for retrieving separately attributes
57  // and corresponding values
58  $regex = '/('.ATT_REG.')="('.VAL_REG.')"/';
59  preg_match_all( $regex, $out[0], $out );
60
61  // Searching and returning the value of the requested attribute
62  for ( $i = 0; $i < sizeof( $out[0] ); $i++ )
63  {
64    if ( $out[1][$i] == $attribute )
65    {
66      return $out[2][$i];
67    }
68  }
69  return '';
70}
71       
72// The function getChild returns the first child
73// exemple : getChild( "<table><tr>XXX</tr><tr>YYY</tr></table>", "tr" )
74//           returns "<tr>XXX</tr>"
75function getChild( $document, $node )
76{
77  $regex = '/<'.$node.'(\s+'.ATT_REG.'="'.VAL_REG.'")*';
78  $regex.= '(\s*\/>|>.*<\/'.$node.'>)/U';
79
80  preg_match( $regex, $document, $out );
81  return $out[0];
82}
83
84// getChildren returns a list of the children identified by the $node
85// example :
86//     getChild( "<table><tr>XXX</tr><tr>YYY</tr></table>", "tr" )
87//     returns an array with :
88//          $array[0] equals "<tr>XXX</tr>"
89//          $array[1] equals "<tr>YYY</tr>"
90function getChildren( $document, $node )
91{
92  $regex = '/<'.$node.'(\s+'.ATT_REG.'="'.VAL_REG.'")*';
93  $regex.= '(\s*\/>|>.*<\/'.$node.'>)/U';
94
95  preg_match_all( $regex, $document, $out );
96  return $out[0];
97}
98       
99// get_CodeXML places the content of a text file in a PHP variable and
100// return it. If the file can't be opened, returns false.
101function getXmlCode( $filename )
102{
103  $file = fopen( $filename, 'r' );
104  if ( !$file )
105  {
106    return false;
107  }
108  while ( !feof( $file ) )
109  {
110    $xml_content .= fgets( $file, 1024 );
111  }
112  fclose( $file );
113  $xml_content = str_replace( "\n", '', $xml_content );
114  $xml_content = str_replace( "\t", '', $xml_content );
115
116  return $xml_content;
117}
118?>
Note: See TracBrowser for help on using the repository browser.