Jump to content

dbol

Members
  • Posts

    12
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

dbol's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. I'm currently using simplexml_load_string to load the xml into the variable, and then a for loop to extract all the records at one time. What I want to do though is extract records 1-50 and place them on page 1, and records 50-100 to be put on page 2. Is this possible? My current for loop looks like: foreach ($items as $item){ echo "<b>" . $item->title . "</b><br/>"; } ?>
  2. I cannot seem to parse special characters correctly. I tried passing it through a converter (iconv) to encode in UTF-8, but it doesn't help. My XML file is as follows: <rss version="2.0"> <channel> <item> <title>Email: Deval Patrick premium caps could cause &#38;#x2018;train wreck&#38;#x2019; - BostonHerald.com</title> <link>http://news.bostonherald.com/business/healthcare/view/20100608email_deval_patrick_premium_caps_could_cause_train_wreck/srvc=home&#38;#x26;amp;position=recent</link> <description>BOSTON &#38;#x2014; A top state insurance official said that health insurance caps the Patrick administration placed on major insurers could cripple them financially and would most likely cause &#38;#x22;a train wreck,&#38;#x22; according to an email obtained Tuesday by The Associated...</description> <pubDate>Tue, 08 Jun 2010 20:56:52 GMT</pubDate> <source url="http://news.bostonherald.com/">BostonHerald.com</source> </item> </channel> </rss> My PHP is as follows: <?php $incoming = file_get_contents("parsexml.xml"); $simplexml= iconv( "UTF-8", "ISO-8859-1//TRANSLIT", $incoming); $simplexml = simplexml_load_string($simplexml); $items = $simplexml->channel->item; ?> <html> <head> <title>Parse Special Chars</title> </head> <body bgcolor="#FFFFFF"> <center><h1>Title</h1></center> <br/> <table align="center"> <tr> <td width="600"> <?php foreach ($items as $item){ echo "<b>" . $item->title . "</b><br/>"; } ?> </tr> </td> </tr> </table> </body> </html> Notice the title has odd characters around 'train wreck' where there should be apostrophes. I've also attached both the xml and php file for convenience. Thanks. [attachment deleted by admin]
  3. I modified the script to parse an RSS2 feed, because our RSS filters duplicate articles, where the XML does not. Only problem now is the single quotes in the RSS are coming out very weird in the PHP; it doesn't happen with normal single quotes; it occurs when the single quotes are slanted, like ‘ and ’, not '. How can I adjust to account for those quotes? Edit: The RSS is encoded in UTF-8.
  4. This helps immensely. Thanks so much.
  5. Looks promising, but perhaps I'm implementing incorrectly. <?php for($x=0;$x<count($document_array);$x++){ $newtitle = htmlspecialchars_decode($document_array[$x]->title, ENT_QUOTES); echo "\t" . $newtitle . "\n<br/>"; echo "<b>\t" . $document_array[$x]->title . "</b>\n<br/>"; $newdate = date('m/j/Y',strtotime($document_array[$x]->date)); echo "\t" . $newdate . " | " . $document_array[$x]->source . "\n<br/>"; echo "\t" . $document_array[$x]->ingress . "\n<br/><br/>"; } ?> Output of lines 4 and 5 are identical. I've tried htmlspecialchars and htmlspecialchars_decode. Both produce the same results, which is exactly what the title is showing in the XML element: amp;T June 7th (after htmlspecialchars) amp;T June 7th (before htmlspecialchars) <title>Metered Data Plans, Tethering Coming To AT&T June 7th</title> (the xml element) Metered Data Plans, Tethering Coming To AT&T June 7th (what I'm seeking to output) Additional info: single quotes are displaying as ' (in xml) double quotes are displaying as " (in xml) ampersands are displaying as & (in xml) These 3 special characters are causing the title to output incorrectly, starting after the last instance of any of those special characters in the title line. So if I have: This & is "the title" now, only now will output as the title.
  6. Anytime I try to extract the title of an article that has special characters, such as & or ', it's only returning the letters after the special characters. For instance. If I have title: <title>MarketTelegraph.com: PVSP & NRDS</title> Then my result is: NRDS rather than MarketTelegraph.com: PVSP & NRDS I need the entire title however. My code is below: <?php $xml_file = "http://www.meltwaternews.com/magenta/xml/html/37/10/150061.html.XML"; $xml_title_key = "*FEEDS*FEED*DOCUMENTS*DOCUMENT*TITLE"; $xml_url_key = "*FEEDS*FEED*DOCUMENTS*DOCUMENT*URL"; $xml_ingress_key = "*FEEDS*FEED*DOCUMENTS*DOCUMENT*INGRESS"; $xml_date_key = "*FEEDS*FEED*DOCUMENTS*DOCUMENT*CREATEDATE5"; $xml_source_key = "*FEEDS*FEED*DOCUMENTS*DOCUMENT*SOURCENAME"; $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/150061.html.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"> <center><h1>Title</h1></center> <br/> <table align="center"> <tr> <td width="600"> <?php for($x=0;$x<count($document_array);$x++){ echo "<b>\t" . $document_array[$x]->title . "</b>\n<br/>"; $newdate = date('m/j/Y',strtotime($document_array[$x]->date)); echo "\t" . $newdate . " | " . $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/>"; } ?> </td> </tr> </table> </body> </html> Ideas?
  7. I got it. Works great. Thanks so much for your help.
  8. Where in the code is that going to go? The dates are all called with a variable, so I can't just pass that specific date string I pasted earlier in to your converter. That was just an example. I have to pass the variable, $date, or something like it.
  9. 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?
  10. Almost a perfect solution... had 1 extra closing paren, and the || needed to be changed to a &&; an otherwise flawless solution. Thanks for your help.
  11. I will try it as soon as I get into work... the filemtime is working now, so that 1 of 2 issues solved. I'll keep you posted; thanks for the reply.
  12. Hello. I've got a piece of code that almost works, minus 2 factors. The first one has to do with with the substr method, and the second is a filemtime issue. The code is as follows, and I will explain: for($index=0; $index < $indexCount; $index++) { if (substr("$dirArray[$index]", 0, 1) != ".") || if (substr("$dirArray[$index]", -3, 3) != "ptn")) { print("<TR><TD width=60%><a href=\"Z:/Clients/$clientid/$dirArray[$index]\">$dirArray[$index]</a></td>"); print("<td width=16%><center><a href='Z:/Clients/$clientid/$dirArray[$index]'>[view]</a> <a href='insurance.php?action=deletefile&clientid=$clientid&file=$dirArray[$index]'>[remove]</a></center></td>"); echo "<td width=40%><center>" . date('M d, Y', filemtime("D:\\Clients\\$clientid\\$dirArray[$index]")) . "</center></td>"; print("</TR>\n"); } } What I'm doing here, is reading a directory using the readdir function, putting the elements into an array called $dirArray, and then cycling through a for loop and displaying all files in that directory. The problem here is, that it also displays hidden files, and my clients do not need to see them. The 2nd line of code: if (substr("$dirArray[$index]", 0, 1) != ".") || if (substr("$dirArray[$index]", -3, 3) != "ptn")) If I remove everything to the right of the OR (||), the code works fine, but I am trying to filter files that end with extension .ptn from displaying. As soon as I put that second substr back in, the page won't load properly. The second issue is with the filemtime function: echo "<td width=40%><center>" . date('M d, Y', filemtime("D:\\Clients\\$clientid\\$dirArray[$index]")) . "</center></td>"; I am trying to cycle through the files in the directory using that for loop, and print the filenames, along with their modified dates, however, the dates all show up as December 31, 1969. When I change the path to an exact path, it works fine, but won't work properly with $dirArray. Just for sake of clarity, below is the while loop used to fill the $dirArray var. while($entryName = readdir($myDirectory)) { $dirArray[] = $entryName; }
×
×
  • 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.