source: tags/release-1_3_0/include/functions_xml.inc.php @ 20335

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

improve the header of each file

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