ugrás a tartalomhoz

"&" értelmezése PHP SAX feldolgozásnál

Anonymous · 2005. Okt. 1. (Szo), 20.50
Most ismerkedem az XML-el. Kezdésnek egy egyszerű RSS olvasót barkácsolnék, de ha az XML fájlban "&" karaktersorozat van, akkor megőrül.
A hibára kihegyezett letisztitott kód:
  1. <?php  
  2.   
  3. $file = "http://hup.hu/backend.php";  
  4.   
  5. function startElement($parser$name$attribs) { // <name>  
  6.       
  7.     global $link;  
  8.       
  9.     if ($name == "LINK")  
  10.         $link = true;     
  11.           
  12. }  
  13.   
  14. function dataHandler($parser$data){ // data  
  15.   
  16.     global $link;  
  17.       
  18.     if ($link)    
  19.         echo $data."<br>";  
  20. }  
  21.   
  22. function endElement($parser$name){ //</name>  
  23.   
  24.     global $link;  
  25.           
  26.     if ($name == "LINK")      
  27.         $link = false;    
  28.           
  29. }  
  30.   
  31. $xml_parser = xml_parser_create();  
  32. xml_set_character_data_handler($xml_parser'dataHandler');   
  33. xml_set_element_handler($xml_parser"startElement""endElement");  
  34.   
  35. if (!($fp = fopen($file"r"))) {  
  36.    die("could not open XML input");  
  37. }  
  38.   
  39. while ($xml_data = fread($fp, 4096)) {  
  40.    if (!xml_parse($xml_parser$xml_datafeof($fp))) {  
  41.        die(sprintf("XML error: %s at line %d",  
  42.                    xml_error_string(xml_get_error_code($xml_parser)),  
  43.                    xml_get_current_line_number($xml_parser)));  
  44.    }  
  45. }  
  46.   
  47. xml_parser_free($xml_parser);   
  48.   
  49. ?>  
Ha a "&amp;" karaktersorozatot str_replace()-el kicserélem pl.: "_"-ra, akkor jó, de enélkül ezt adja egy linkre:
http://www.hup.hu/modules.php?name=News
&
file=article
&
sid=9773
&
mode=nested

és ugye ennek kellene lenni:
http://www.hup.hu/modules.php?name=News&file=article&sid=9773&mode=nested

olyan mintha minden "&amp;"-ra újra meghívná a dataHandler()-t. Mi lehet a gond?

üdv.: Zsolt