Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

XML Parser reading html tags

  • 21-07-2010 01:46PM
    #1
    Registered Users, Registered Users 2 Posts: 8,070 ✭✭✭


    [PHP]<post>
    <regular-title>this is another test</regular-title>
    <regular-body><b>oh my lols</b></regular-body>
    </post>
    [/PHP]

    [PHP]<?php
    $insideitem = false;
    $tag = "";
    $title = "";
    $description = "";
    $link = "";

    function startElement($parser, $name, $attrs) {
    global $insideitem, $tag, $title, $description, $link;
    if ($insideitem) {
    $tag = $name;
    } elseif ($name == "POST") {
    $insideitem = true;
    }
    }

    function endElement($parser, $name) {
    global $insideitem, $tag, $title, $description, $link;
    if ($name == "POST") {


    echo $title."<br>";
    echo $description;

    $title = "";
    $description = "";
    $link = "";
    $insideitem = false;
    }
    }

    function characterData($parser, $data) {

    global $insideitem, $tag, $title, $description, $link;
    if ($insideitem) {

    switch ($tag) {
    case "REGULAR-BODY":
    $description .=$data;
    break;

    case "REGULAR-TITLE":
    $title .= $data;
    break;

    }
    }
    }

    $xml_parser = xml_parser_create();

    xml_set_element_handler($xml_parser, "startElement", "endElement");
    xml_set_character_data_handler($xml_parser, "characterData");


    $fp = fopen("my.xml","r")
    or die("Error reading RSS data.");
    while ($data = fread($fp, 4096))

    xml_parse($xml_parser, $data, feof($fp))
    or die(sprintf("XML error: %s at line %d",
    xml_error_string(xml_get_error_code($xml_parser)),
    xml_get_current_line_number($xml_parser)));
    fclose($fp);
    xml_parser_free($xml_parser);

    ?>[/PHP]


    case "REGULAR-TITLE":
    works

    case "REGULAR-BODY":
    doesnt

    its because of the <b> tag
    i tried using htmlentities but wont work :/
    cant use SIMPLEXMLLOAD because hosting only has php4 :mad:


Advertisement