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]

Leave a Reply