Yuav Ua Li Cas Ua Ib Tug Parser

Cov txheej txheem:

Yuav Ua Li Cas Ua Ib Tug Parser
Yuav Ua Li Cas Ua Ib Tug Parser

Video: Yuav Ua Li Cas Ua Ib Tug Parser

Video: Yuav Ua Li Cas Ua Ib Tug Parser
Video: Tsis Muaj Peev Xwm Yuav Ua Li Cas 2024, Kaum ib hlis
Anonim

Parsing yog ib qho ntawm cov txuj ci siv dav hauv qhov web page programming. Nws yooj yim thiab yooj yim tso cai rau koj kom tau txais cov kev pabcuam tsim nyog rau lub xaib siv cov ntawv sau me me, thaum tsis muaj txoj hauv kev los sau cov ntawv tsim nyog koj tus kheej.

Yuav ua li cas ua ib tug parser
Yuav ua li cas ua ib tug parser

Cov Lus Qhia

Kauj ruam 1

Qhov yooj yim tshaj tawm yog nrog cov PHP muaj nuj nqi file_get_contents (). Nws tso cai rau koj kom tau cov ntawv ntawm cov ntaub ntawv ua kab ntawv. Txoj haujlwm siv lub "nco daim phiaj" algorithm, uas txhim kho nws cov kev ua haujlwm.

Kauj ruam 2

Piv txwv li, txhawm rau sau ib tsab ntawv uas txheeb xyuas cov ntaub ntawv los ntawm lub vev xaib ntawm Central Bank ntawm Lavxias Kev Tshawb Fawb, koj yuav tsum tau txais cov ntsiab lus ntawm nplooj ntawv XML siv cov haujlwm tsim nyog, muaj yav tas los txhais cov hnub tim hauv hom ntawv uas tsim nyog rau lub xaib, thiab tom qab ntawd siv cov ntawv qhia tas li los cais nws. Txhawm rau nthuav tawm cov nyiaj uas tau xaiv, cov cai tau los ntawm Lub Txhab Nyiaj Keeb Kwm lub website tau siv: $ data = hnub (“d / m / Y”); $ tau = file_get_contents (https://www.cbr.ru/scripts/XML_daily.asp ? date_req = $ data); preg_match (“/(.*?) / yog”, $ tau, $ txoj hlua); preg_match (“/(.*?) / yog”, $ hlua [1], $ str);

Kauj ruam 3

Yog tias koj xav tawm ntawm cov ntawv XML nws tus kheej, kuj tseem muaj lub zog ua haujlwm rau qhov no. Txhawm rau pib parser, koj yuav tsum pib nws siv xml_parser_create: $ parser = xml_parser_create ();

Kauj ruam 4

Tom qab ntawd ib daim ntawv teev lub luag haujlwm tau teev tseg uas yuav ua cov cim coj thiab cov ntawv sau. Tus XML XML sib raug pib thiab xaus handlers raug teeb tsa: xml_set_element_handler ($ parser, "startElement", "endElement");

Kauj ruam 5

Cov ntaub ntawv tuaj yeem nyeem tau uas siv tus qauv fopen () thiab fgets () ua haujlwm hauv lub voj uas tsim nyog. Cov ntsiab lus ntawm cov ntaub ntawv xa rov qab kab los ntawm kab hauv xml_parse (). Qhov ntsuas dhau los muaj tus chij ntawm nyeem kab kawg: thaum ($ content = fgets ($ fparse)) {

yog (! xml_parse ($ parser, $ ntsiab lus, feof ($ fparse))) {

ncha “Yuam Kev”;

tawg;.}}

Kauj Ruam 6

Qhov xml_parser_free () muaj nuj nqi yog siv los tso khoom pub dawb los ntawm lub zog. Cov haujlwm no muaj zog tshaj thaum ua cov ntaub ntawv XML.

Pom zoo: