note 68171 deleted from function.xml-parse-into-struct by cmb

From: Date: Sun, 21 Oct 2018 09:13:45 +0000
Subject: note 68171 deleted from function.xml-parse-into-struct by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-209473@lists.php.net to get a copy of this message
Note Submitter: webmaster at unitedscripters dot com ---- It may be not entirely immaterial to stress that when you are dealing with incoming XML files such as RSS feeds, and you are about to include several of them in some page of yours, resorting to the PHP XML oriented functions is neither _necessarily_ the best idea, nor it is _strictly_ indispensable. I have in mind, here, also a note that time ago was on this documentation by some info at gramba dot tv: QUOTE I was working with the xml2array functions below and had big performance problems. I fired them on a 20MB XML file and had to quit since all approaches of parsing where just too slow (more than 20 Minute parsing etc..). The solution was parsing it manually with preg_match, which increased performance by more than 20 times (processing time about 1 minute). UNQUOTE Calling in a specific XML structure function, and arranging a whole class, when all you want from an incoming files may be the contents of a few tags, is not the only option you are left with, when you are at PHP. Here is a simple function that parses a XML RSS feed using no XML oriented function: keeping this in mind may spare you the need to create extremely complex classes as the ones we see here when _all_ you may want is a few titles and descriptions from an RSS (if that's your goal, you don't need XML parsers): <?php function rssSnapper($input='', $limit=0, $feedChannel='Yahoo!News'){ $input=file_get_contents($input); if(!$input){return '';}; $input=preg_replace("/[\\n\\r\\t]+/", '', $input); $input=preg_replace("/(<!\\[CDATA\\[)|(\\]\\]>)/", '', $input); preg_match_all("/<item>(.*?)<\\/item>/", $input, $items, PREG_SET_ORDER); $limit=(int)$limit; $limit=($limit && is_numeric($limit) && abs($limit)<sizeof($items))? sizeof($items)-abs($limit): 0; while(sizeof($items)>$limit){ $item=array_shift($items); $item=$item[1]; preg_match_all("/<link>(.*?)<\\/link>/", $item, $link, PREG_SET_ORDER); preg_match_all("/<title>(.*?)<\\/title>/", $item, $title, PREG_SET_ORDER); preg_match_all("/<author>(.*?)<\\/author>/", $item, $author, PREG_SET_ORDER); preg_match_all("/<pubDate>(.*?)<\\/pubDate>/", $item, $pubDate, PREG_SET_ORDER); preg_match_all("/<description>(.*?)<\\/description>/", $item, $description, PREG_SET_ORDER); if(sizeof($link)){ $link = strip_tags($link[0][1]); }; if(sizeof($title)){ $title = strtoupper( strip_tags($title[0][1]) ); }; if(sizeof($author)){ $author = strip_tags($author[0][1]); }; if(sizeof($pubDate)){ $pubDate = strip_tags($pubDate[0][1]); }; if(sizeof($description)){ $description = strip_tags($description[0][1]); }; print <<<USAVIT <!-- ITEM STARTS --> <div class="news_bg_trick"> <a href="$link" class="item" target="_blank"> <span class="title">$title<span class="channel">$feedChannel</span></span> <span class="title_footer"> by <span class="author">$author</span> - <span class="date">$pubDate</span> </span> <span class="description">$description</span> </a> </div> <!-- ITEM ENDS --> USAVIT; }//out of loop /*unitedscripters.com*/} ?> The printing phase assigns Css class names: the output is thus fully customizable by a mere style sheet. The use of strip_tags is a reminder from Chris Shiflett: distrust incoming data, always, anyway. I hope no typos slipped in in transcription. Arguably not perfect, but I hope a good alternative idea to spending three days on a full fledged XML parser just to grab... three tags from a RSS feed! bye, ALberto

« previous php.notes (#209473) next »