Maisonnette » The Meh!


PHP – RSS/Atom feed parser – DOM

Posted in Scripts by The Meh! on the January 30th, 2009

A way to parse RSS or ATOM feeds using “Document Object Model” and output it into array:

<?php
function rss_to_array($tags, $array, $url) {
  /*RSS and ATOM*/
  $doc = new DOMdocument();
  @$doc->load($url);
  $rss_array = array();
  foreach($tags as $tag) {
    if ($doc->getElementsByTagName($tag)) {
      foreach($doc->getElementsByTagName($tag) AS $node) {
        $items = array();
        foreach($array AS $key => $values) {
          $items[$key] = array();
          foreach($values as $value) {
            if ($itemsCheck = $node->getElementsByTagName($value)) {
              for( $j=0 ;  $j < $itemsCheck->length; $j++ ) {
                if (($attribute = $itemsCheck->item($j)->nodeValue) != "") {
                  $items[$key][] = $attribute;
                } else if ($attribute = $itemsCheck->item($j)->getAttribute('term')) {
                  $items[$key][] = $attribute;
                } else if ($itemsCheck->item($j)->getAttribute('rel') == 'alternate') {
                  $items[$key][] = $itemsCheck->item($j)->getAttribute('href');
                }
              }
            }
          }
        }
        array_push($rss_array, $items);
      }
    }
  }
  return $rss_array;
}

$rss_item_tags = array('item', 'entry');
$rss_tags = array(
  'title' => array('title'),
  'description' => array('description', 'content', 'summary'),
  'link' => array('link', 'feedburner'),
  'category' => array('category')
);

$rssfeed = rss_to_array($rss_item_tags, $rss_tags, $url);

echo '&lt;pre&gt;';
print_r($rssfeed);
echo '&lt;/pre&gt;';
exit;
?&gt;
Comments Off

PHP – Get url variables case insensitive

Posted in Scripts by The Meh! on the December 23rd, 2008

$str = $_SERVER['QUERY_STRING'];

parse_str($str, $output);

foreach($output as $key =&gt; $value) {
$QUERY[strtolower($key)] = $value;
}

print_r($QUERY);

Example: url:

http://www.site.com/index.php?ID=C22&Var2=DA&vAr3=DA&LINK=Test

Array
(
[id] =&gt; C22
[var2] =&gt; DA
[var3] =&gt; DA
[link] =&gt; Test
)

Comments Off

Generate a unique ID 10 characters long – PHP

Posted in Scripts by The Meh! on the September 11th, 2008

A PHP function to generate a unique ID 10 characters long based on the current UNIX timestamp (10 digits).

<?php
function gen_uniqid() {
	$char[0] = "ABCDEFGHIJ";
	$char[1] = "KLMNPQRSTU";
	$char[2] = "VWXYZabcde";
	$char[3] = "fghijklmnp";
	$char[4] = "qrstuvwxyz";
	$char[5] = "0123456789";
	$arr = preg_split('//', time(), -1, PREG_SPLIT_NO_EMPTY);

	$uniqid = "";
	foreach($arr as $val) {
		$str = $char[rand(0,5)];
		$uniqid .= $str{$val};
	}

	return $uniqid;
}
echo gen_uniqid();
?>