So lost, xml, php, arrays help pls!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • silvrique@gmail.com

    #1

    So lost, xml, php, arrays help pls!

    I am totally lost.

    I have an xml script example as follows:

    <?xml version="1.0" encoding="utf8" ?>
    <starter>
    <second>
    <name>Silver</name>
    </second>
    </starter>

    I have this printing to an array with the following php code:

    class RSSParser {

    var $struct = array(); // holds final structure
    var $curptr; // current branch on $struct
    var $parents = array(); // parent branches of current branch

    function RSSParser($url) {
    $this->curptr =& $this->struct; // set ref to base
    $xmlparser = xml_parser_crea te();
    xml_set_object( $xmlparser, $this);
    xml_set_element _handler($xmlpa rser, 'tag_open', 'tag_close');
    xml_set_charact er_data_handler ($xmlparser, 'cdata');
    $fp = fopen($url, 'r');

    while ($data = fread($fp, 4096))
    xml_parse($xmlp arser, $data, feof($fp))
    || die(sprintf("XM L error: %s at line %d",
    xml_error_strin g(xml_get_error _code($xmlparse r)),
    xml_get_current _line_number($x mlparser)));

    fclose($fp);
    xml_parser_free ($xmlparser);
    }

    function tag_open($parse r, $tag, $attr) {
    $i = count($this->curptr['children']);
    $j = count($this->parents);
    $this->curptr['children'][$i]=array(); // add new child element
    $this->parents[$j] =& $this->curptr; // store current position as
    parent
    $this->curptr =& $this->curptr['children'][$i]; // submerge to
    newly created child element
    $this->curptr['name'] = $tag;
    if (count($attr)>0 ) $this->curptr['attr'] = $attr;
    }

    function tag_close($pars er, $tag) {
    $i = count($this->parents);
    if ($i>0) $this->curptr =& $this->parents[$i-1]; // return to
    parent element
    unset($this->parents[$i-1]); // clear from list of parents
    }

    function cdata($parser, $data) {
    $data = trim($data);
    if (!empty($data)) {
    $this->curptr['value'] .= $data;
    }
    }

    }

    $myparser = new RSSParser('Cred itCardBank.xml' );

    echo "<pre>";
    $results = ($myparser->struct);
    print_r($result s);

    -----

    I get the following array:

    [1] => Array
    (
    [name] => STARTER
    [children] => Array
    (
    [0] => Array
    (
    [name] => NAME
    [value] => Silver
    Person&
    )

    )

    )

    -------

    I need to be able to take the above array results and use them to
    populate a menu, yet at the same time create ANOTHER array that
    cleanses the : [value] => Silver Person& to remove the capitol
    letters, spaces and charachters other than hyphens, letters and
    numbers. I need the new array to show something like Silver Person& ==>
    SilverPerson

    Is this EVEN possible??

Working...