opera mobile

i have to say that, so far, opera mobile (demo/beta mind you) is the best web browser available for my ppc6700 phone. minimo (ppc mozilla port) would win out except its slow (read SLOW) and crashes a lot. opera mobile is quick, responsivee, has tabs, and works with wordpress (how i’m posting this now!) thunderhawk was a promising cpntender but turned out to be a joke (to be fair my pho ne isnt supported)… at this point i’m stuck in IE hell… and it sucks…now… to find a *good* ssh client with dsa public key auth support

A class for normalizing mixed value containers

There are times (like when dealing with simplexml) when you just wish you had an array that you can iterate over for whatever your reasons are (especially when dealing with variable structured multidimensional unpredictable input.) This is also a good example of recursive functions, simplification of difficult specialized problems, and how one might use a class to accomplish large tasks in an encapsulated fashion.
[coolcode]

/**
* A class for [N]ormalizing a [C]ontainer to an array
*
* This will class will take any type of variable container and return a normalized array
* For example this will take the output of simplexml_load_string and render it all into
* a multidimensional array.
*
*
*/

/**
*
*/

class nc2array {

private $original_container;
private $normalized_container;

/*
* Takes a variable, and builds a normalized comtainer out of it
*/
function __construct($container) {
$this->original_container=$container;
if ( is_object($container) ) {
$this->normalized_container=$this->recursive_parse_object($container);
} elseif ( is_array($container) ) {
$this->normalized_container=$this->recursive_parse_array($container);
} else {
$this->normalized_container[]=$container;
}
return($this->normalized_container);
}

/*
* takes an array and parses it recursively, passing objects off to recursive_parse_object
*/
function recursive_parse_array($array) {
foreach ( $array as $idx => $val ) {
if ( is_array($val) ) {
$rval[$idx]=$this->recursive_parse_array($val);
} elseif( is_object($val) ) {
$rval[$idx]=$this->recursive_parse_object($val);
} else {
$rval[$idx]=$val;
}
}
return($rval);
}

/*
* Takes an object and parses it recursively, passing arrays back off to recursive_parse_array
*/
function recursive_parse_object($obj) {
$vals=get_object_vars($obj);
while (list($idx, $val) = each($vals)) {
if ( is_object($val) ) {
$rval[$idx]=$this->recursive_parse_object($val);
} elseif ( is_array($val) ) {
$rval[$idx]=$this->recursive_parse_array($val);
} else {
$rval[$idx]=$val;
}
}
return($rval);
}
}
?>

[/coolcode]

Building and using large scale arrays

Here’s a real world example of what I was talking about earlier (using all parts of an array as relevant data) This is a small piece of some blogging related code I wrote earlier this week for Ookles (this is a part of a code package that will be made generally available when it’s ready)
[coolcode]

function get_html_metadata($url) {
/*
Example usage/test code

$test_urls[]=”http://jwz.livejournal.com/”;
$test_urls[]=”http://fuzzyblog.wordpress.com/”;
foreach ( $test_urls as $url ) {
$meta_tags=o_get_meta_tags($url);
foreach ( $meta_tags as $tag_type => $val ) {
echo “Tag Type: {$tag_type}
“;
foreach ( $val as $tag_type_number => $val ) {
echo “\t#{$tag_type_number}
“;
foreach ( $val as $tag_meta_key => $val ) {
echo “\t\t{$tag_meta_key}:\t”;
foreach ( $val as $tag__meta_value ) {
echo “{$tag__meta_value} “;
}
echo ”
“;
}
}
}
}

*/
$data=trim(file_get_contents($url));
$data=str_replace(chr(10), ”, $data);
$data=str_replace(‘/>’, ‘>’, $data);
$data=str_replace(‘>’, ‘>’.chr(10), $data);
$data=explode(chr(10), $data);
$meta_array=array();
// Loop through the different tags matching only tags with an = sign in them
foreach ( $data as $line ) {
if (ereg(‘<([^ ]+)[ ]+(.*=.*)>‘, $line, $r) ) {
$meta_type=$r[1];
$meta_counter[$meta_type]++;
$current_meta_counter=$meta_counter[$meta_type];
$mini_data=str_replace(‘=”‘, ‘=’, $r[2]);
$mini_data=str_replace(“='”, ‘=’,$mini_data);
$mini_data=str_replace(‘”‘, chr(10), $mini_data);
$mini_data=str_replace(“‘”, chr(10), $mini_data);
$mini_data=explode(chr(10), trim($mini_data));
// for debugging: $meta_array[$meta_type][$current_meta_counter][‘original’][]=$r[0];
foreach ( $mini_data as $val ) {
$val=explode(‘=’, trim($val));
$meta_key=$val[0];
$meta_val=$val[1];
// build our array
$meta_array[$meta_type][$current_meta_counter][$meta_key][]=$meta_val;
}
}
}
if ( count($meta_array) > 0 ) {
return($meta_array);
} else {
return(FALSE);
}
}
[/coolcode]

This might be of use to someone

I’ve been writing PHP for a long time now. And one of the things that I see quite often is that people just dont get multidimensional arrays, and how to effectively store data in them for looping. Allow me to lead by example.

  • $store[‘fruit’][‘apple’]=3;
  • $store[‘fruit’][‘orange’]=1;
  • $store[‘berries’][‘blueberries’]=100;
  • $store[‘berries’][‘strawberries’]=100;
  • $store[‘fruit’][‘mango’]=5;
  • foreach ( $store as $type => $val ) {
    • foreach ( $val as $name => $qty ) {
      • echo “We have {$qty} {$name} ({$type} are good for you)rn”;
    • }
  • }

of course where the array comes from and what you do with it are completely optional. But it’s important to know that the array indexes should be as informational as the array values. Otherwise you arent using arrays properly.

Generic function/class names

This is especially true when you’re writing a library which is meant to be absorbed into a larger codebase, but also true of a codebase in which you know you will be using foreign libraries to accomplish tasks. And I’m as guilty as anybody when it comes to this!
When you are naming your functions and classes be mindfull of the possibility of collision. I think that we (as a group of programmers) are generally mindful of this when we lay out things like our database schema, but can overlook this when we’re writing our libraries.
For example a class/function name of something like “database” seems good ad first glance: It’s clear, to the point, and descriptive (okay… *somewhat* descriptive). However consider that anyone who’se writing anything even remotely related to a database (and these days what *isnt* tied to a database?) will think to themselves at one point “hmm if i name the class database, it’ll be short enough not to be annoying to type, long enough to describe what its used for, and no one’s going to think its used for processing text strings!”

So, now, its possible that every library has a right to use this class name for their code because of its qualifications. But then you will be limited to only using one persons external libs (assuming that your internal libs arent already using it)

What we *ought* to do is, for our project Foo, call the class “databaseFoo”, then we can simply use something like $database = new databaseFoo; and we loose basically nothing, and be assured of compatibility with other libs.

DK

Playing with loopholes

The most amusing things happen when you’re been coding a long time. I found, and used a “hole” in one of our database routines (which has since been fixed)

getOneValueFromTable($srcTable, $srcField, $whereField, $whereValue);
produces SQL like “SELECT $srcField FROM $srcTable WHERE $whereField = ‘$whereValue'”. Can you spot the potential problems in something like this? Consider this: getOneValkueFromTable($srcTable, $srcField, ‘1’, ‘1’; delete from $srcTable; ”);

Granted its not much of a problem if only proper developers are able to use this function, but if any untrusted party were able to affect any of those variables… big problems…

Everyone should know this. If you didnt know this kind of a problem existed… consider yourself warned. This has been a public service announcement. We now return you to the normal silence found on this blog

😉

cheers
DK