Jump to content

How do I substring a variable in an array?


dbol

Recommended Posts

Here is my code:

<?php 

$xml_file = "http://www.meltwaternews.com/magenta/xml/html/37/10/rss/147839.rss.XML"; 

$xml_title_key = "*RDF:RDF*ITEM*TITLE";
$xml_url_key = "*RDF:RDF*ITEM*LINK";
$xml_ingress_key = "*RDF:RDF*ITEM*DC:DESCRIPTION";
$xml_date_key = "*RDF:RDF*ITEM*DC:DATE";
$xml_source_key = "*RDF:RDF*ITEM*DC:SOURCE";

$document_array = array(); 

$counter = 0; 
class xml_document{ 
    var $title, $url, $ingress, $date, $source, $dateMonth, $dateDay, $dateYear;
} 

function startTag($parser, $data){ 
    global $current_tag; 
    $current_tag .= "*$data"; 
} 

function endTag($parser, $data){ 
    global $current_tag; 
    $tag_key = strrpos($current_tag, '*'); 
    $current_tag = substr($current_tag, 0, $tag_key); 
} 

function contents($parser, $data){ 
    global $current_tag, $xml_title_key, $xml_url_key, $xml_ingress_key, $xml_date_key, $xml_source_key, $counter, $document_array; 
    switch($current_tag){ 
        case $xml_title_key: 
            $document_array[$counter] = new xml_document(); 
            $document_array[$counter]->title = $data; 
            break; 
        case $xml_url_key:  
            $document_array[$counter]->url = $data; 
            break; 
        case $xml_ingress_key: 
            $document_array[$counter]->ingress = $data; 
            break; 
        case $xml_date_key: 
            $document_array[$counter]->date = $data; 
            break; 	
	case $xml_source_key:
		$document_array[$counter]->source = $data;
		$counter++;
		break;
    } 
}

$xml_parser = xml_parser_create(); 

xml_set_element_handler($xml_parser, "startTag", "endTag"); 

xml_set_character_data_handler($xml_parser, "contents"); 

$fp = fopen($xml_file, "r") or die("Could not open file");

function remotefsize($furl) {
        $sch = parse_url($furl, PHP_URL_SCHEME);
        if (($sch != "http") && ($sch != "https") && ($sch != "ftp") && ($sch != "ftps")) {
            return false;
        }
        if (($sch == "http") || ($sch == "https")) {
            $headers = get_headers($furl, 1);
            if ((!array_key_exists("Content-Length", $headers))) { return false; }
            return $headers["Content-Length"];
        }
    }

$data = fread($fp, remotefsize("http://www.meltwaternews.com/magenta/xml/html/37/10/rss/147839.rss.XML")) or die("Could not read file"); 

if(!(xml_parse($xml_parser, $data, feof($fp)))){ 
    die("Error on line " . xml_get_current_line_number($xml_parser)); 
} 

xml_parser_free($xml_parser); 

fclose($fp); 

?> 

<html> 
<head> 
<title>Project: Parse XML 4</title> 
</head> 
<body bgcolor="#FFFFFF"> 
<?php
for($x=0;$x<count($document_array);$x++){ 
    echo "<b>\t" . $document_array[$x]->title . "</b>\n<br/>";
echo "\t" . $document_array[$x]->date . " | " . $document_array[$x]->source . "\n<br/>";
echo "\t" . $document_array[$x]->ingress . "\n<br/>";
echo "\t<a href='" . $document_array[$x]->url . "'>"  . $document_array[$x]->url . "</a>\n<br/><br/>";
} 
?> 

</body> 
</html>

 

I want to substring the date so that I can extract the year, date, and month as separate variables, and then reformat them to MM/DD/YYYY.

The date currently looks like: 2010-05-29T23:52:40+00:00

I want it to look like 05/29/2010

 

Any ideas?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.