PHP to read RRs

Source: Internet
Author: User
Previously, because of the need to do the school forum, has written a read Shou forum RRs of a section of PHP, write out for everyone to reference


Here is the definition function to extract the data:

The property that handles the start tag refers to the

$attrs is a multidimensional array, the key value is the property name, and the value is the value of the property

function startelement ($parser, $element _name, $attrs) {

Global $temp, $current _tag;

$current _tag = $element _name;

Switch ($element _name) {

Case ' ITEM ':

Echo ';

break;

Default:

break;

}



}



$current _tag tells us the tag we're working on, we'll use it later in the Characterdata function.



function EndElement ($parser, $element _name, $attrs) {

Global $temp, $current _tag;

Switch ($element _name) {

Case ' ITEM ':

Return_page ($temp);

$temp = ";

break;

Default:

break;

}

}



The data that is passed to this function is between the elements

For example, for<title>Title here</title>, $data is equal to ' Title here '

function Characterdata ($parser, $data) {

Global $current _tag, $temp, $catID;

Switch ($current _tag) {

Case ' TITLE ':

$temp [' title '] = $data;

$current _tag = ";

break;

Case ' LINK ':

$temp [' url '] = $data;

$current _tag = ";

break;

Case ' AUTHOR ':

$temp [' author '] = $data;

$current _tag = ";

Case ' DESCRIPTION ':

$temp [' description '] = $data;

$current _tag = ";

Default:

break;

}

}

?>








function Return_page () {

Global $temp;

Echo ' O '. $temp [' title ']. '
' ;


}



The content of the analysis, known as RRS, can be a network location, or it can be a file

$xml _file = ' http://club.sohu.com/list-70th-0-0.xml ';



Define character Set, default is UTF-8

$type = ' UTF-8 ';



Set up a parser

$xml _parser = Xml_parser_create ();



Set resolution options

Xml_parser_set_option ($xml _parser, xml_option_case_folding, true);

Xml_parser_set_option ($xml _parser, xml_option_target_encoding, ' UTF-8 ');



Tell PHP what function to invoke when discovering elements

These functions also handle the attributes of the element

Xml_set_element_handler ($xml _parser, ' startelement ', ' endElement ');



Tell PHP what function to call the character data

Xml_set_character_data_handler ($xml _parser, ' characterdata ');



if (! ($fp = fopen ($xml _file, ' R '))) {

Die ("Due to network problems, temporarily unable to obtain data, please wait to refresh and try again");

}



To parse the entire file through a loop


while ($data = Fread ($fp, 1024))

{

Xml_parse ($xml _parser, $data, feof ($fp));

}

Fclose ($FP);

Xml_parser_free ($xml _parser);



?>

Alternatively, you can add a script for the JavaScript that is used to pre-load the Web page

< SCRIPT LANGUAGE = "JavaScript" >





< SCRIPT LANGUAGE = "JavaScript" >



< style type = "Text/css" >





< div id = "LD" style = "position:absolute; left:0px; top:0px; width:100%; height:100%; Background-color: #ffffFF; z-index:1000; ">

< br >

< br >< br >< br >< br >< br >< br >< br >< br >< br >< BR &GT;&L T br >

< table align = "center" width = "50%" >

< tr >

< td align = "center" >< Font color = #0000ff >< B > Sohu Forum < br >

< br >

< font color = "#0000ff" > Loading Please wait a moment ...





< table width = "50%" align = "center" border = "0" cellspacing = "1" cellpadding = "0"

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.