Convertire una data RFC822 in una normale
by info@fabioricali.it on giu.09, 2010, under PHP
Questa funzione ci permette di convertire una data GMT in un formato data normale
<?php function RFC822ToDate($gmtDate, $format="it", $showTime=true){ list($dayT, $dayN, $monthT, $year, $time) = explode(" ",$gmtDate); $dayT = trim($dayT,","); $months = array("01"=>"Jan","02"=>"Feb","03"=>"Mar","04"=>"Apr","05"=>"May","06"=>"Jun","07"=>"Jul","08"=>"Aug","09"=>"Sep","10"=>"Oct","11"=>"Nov","12"=>"Dec"); $monthT = array_search($monthT, $months); if(!$showTime) $time = ""; switch($format){ case "it": $date = $dayN."-".$monthT."-".$year." ".$time ; break; case "us": $date = $year."-".$monthT."-".$dayN." ".$time ; break; default: $date = $year."-".$monthT."-".$dayN." ".$time ; break; } return $date; } echo RFC822ToDate("Tue, 08 Nov 2014 06:47:10 GMT"); //stampa 08-11-2014 06:47:10 ?>
Leggere feed rss con DOM XML php
by info@fabioricali.it on giu.09, 2010, under PHP
Questa classe ci permette di leggere un RSS sfruttando la libreria DOM xml introdotta da PHP 5.
Il metodo principale è getRss con tre paramentri:
$urlXml (url al file xml)
getRss(“http://sito.it/rss.xml”)
$index di default è null (un numero corrispondente ad un nodo, es: se volessimo estrarre il nodo 5)
getRss(“http://sito.it/rss.xml”,5)
$offset di default é null (imposta assieme a $index il range di nodi da estarre, es: dal nodo 1 al 5)
getRss(“http://sito.it/rss.xml”,1,5)
<?php /** * rss * * @package RssNow * @author Fabio Ricali * @copyright 2010 * @version 1.0.0 * @access public */ class RssNow{ /** * rss::getRss() * * @param string $urlXml * @param int $index * @param int $offSet * @return array */ public function getRss($urlXml, $index=null, $offSet=null){ $doc = new DOMDocument(); $doc->load($urlXml); $items = $doc->getElementsByTagName("item"); $i = 0; foreach( $items as $item ){ $i++; $titles = $item->getElementsByTagName("title"); $title = $titles->item(0)->nodeValue; $pubdates = $item->getElementsByTagName("pubDate"); $pubdate = $pubdates->item(0)->nodeValue; $descriptions = $item->getElementsByTagName("description"); $description = $descriptions->item(0)->nodeValue; $categories = $item->getElementsByTagName("category"); $category = $categories->item(0)->nodeValue; $authors = $item->getElementsByTagName("author"); $author = $authors->item(0)->nodeValue; $comments = $item->getElementsByTagName("comments"); $comment = $comments->item(0)->nodeValue; $links = $item->getElementsByTagName("link"); $link = $comments->item(0)->nodeValue; $enclosures = $item->getElementsByTagName("enclosure"); foreach($enclosures as $enclosure_){ $enclosure = $enclosure_->getAttribute("url"); } $eval=' $array[$i][\'title\'] = $title ; $array[$i][\'description\'] = $description ; $array[$i][\'pubDate\'] = $this->gmtToDate($pubdate) ; $array[$i][\'author\'] = $author ; $array[$i][\'comments\'] = $comment ; $array[$i][\'category\'] = $category ; $array[$i][\'enclosure\'] = $enclosure ; $array[$i][\'link\'] = $link ; '; if($index==null && $offSet==null){ eval($eval); }else if($index!=null && $offSet!=null){ $range = range($index,$offSet); if(in_array($i,$range)){ eval($eval); } }else{ if($i==$index){ $eval = str_replace('[$i]','',$eval); eval($eval); return $array; } } } //$array[0]['length'] = $items->length; return $array; } private function gmtToDate($gmtDate, $format="it", $showTime=true){ list($dayT, $dayN, $monthT, $year, $time) = explode(" ",$gmtDate); $dayT = trim($dayT,","); $months = array("01"=>"Jan","02"=>"Feb","03"=>"Mar","04"=>"Apr","05"=>"May","06"=>"Jun","07"=>"Jul","08"=>"Aug","09"=>"Sep","10"=>"Oct","11"=>"Nov","12"=>"Dec"); $monthT = array_search($monthT, $months); if(!$showTime) $time = ""; switch($format){ case "it": $date = $dayN."-".$monthT."-".$year." ".$time ; break; case "us": $date = $year."-".$monthT."-".$dayN." ".$time ; break; default: $date = $year."-".$monthT."-".$dayN." ".$time ; break; } return $date; } } ?>
Utilizzo:
<?php $rss = new RssNow; $arr = $rss->getRss("http://www.repubblica.it/rss/cronaca/rss2.0.xml"); foreach($arr as $item){ echo " Titolo: ".$item['title']."<br>"; echo " Descrizione: ".$item['description']."<br>"; echo " Data pubbl.: ".$item['pubDate']."<br>"; echo " Autore: ".$item['author']."<br>"; echo " Categoria: ".$item['category']."<br>"; } ?>
Redirect in base alla lingua del browser con php
by info@fabioricali.it on mag.16, 2010, under PHP
<?php $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); switch ($lang) { case "it": header("location: it/"); break; case "en": header("location: en/"); break; default: header("location: en/"); break; } ?>
Ottenere l’url corrente con php
by info@fabioricali.it on mag.11, 2010, under PHP
Questa semplicissima funzione vi permettera di ottenere l’url corrente
<?php function getCurrentUrl(){ return "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; } echo getCurrentUrl(); ?>
Rimuovere i tag HTML e PHP da una stringa
by info@fabioricali.it on mag.11, 2010, under PHP
La funzione strip_tags nativa in php permette di rimuovere i tag html e php da una stringa
echo strip_tags("Testo con <strong>HTML</strong>"); //stampa "Testo con HTML"