A while back I used a function I found on the web to print off a binary representation of a number while I was working on a blowfish implementation in C++. I haven’t finished the blowfish, but I made some changes to the binary function, and thought I’d post it since it is a handy thing to have.
Essentially all I did was set it so it grouped the digits by 4 and padded out the MSB set with 0’s so they all line up.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | void binary(int number,int count=0) { int remainder; count++; if(number <= 1) { while(count % 4 != 0) { cout << '0'; count++; } cout << number; return; } remainder = number%2; binary(number >> 1, count); if(count % 4 == 0) cout << ' '; cout << remainder; } |
Here’s an example program, and the results.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include <iostream> using namespace std; void binary(int number,int count=0) { ... } int main () { binary(0); cout << endl; binary(0xFF); cout << endl; binary(9872); cout << endl; } |
jmhobbs@lizzy$ ./a.out 0000 1111 1111 0010 0110 1001 0000 jmhobbs@lizzy$
Got bored and scratched another itch I’ve had in wordpress for a while. I always wanted a way to list my categories on the sidebar on my terms. The best I had found was the well written and executed Category Visibility-RH but I wanted to choose from all my categories and still limit the number listed.
I added a function to my wp-hacks.php and then dropped it into my sidebar. Does the job, and could easily be edited. I swear someday I’ll learn how to make plug-ins, really.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | function jh_random_cats($howMany) { global $wpdb ; $query = " SELECT cat_ID, cat_name, category_count FROM $wpdb->categories WHERE category_count != '0' "; $categories = $wpdb->get_results($query); shuffle($categories); // Mix 'em up for($howMany; $howMany > 0; $howMany--) { $poppedCat = array_pop($categories); print '<li><a href="'.get_category_link($poppedCat->cat_ID).'">'.$poppedCat->cat_name.'</a> ('.$poppedCat->category_count.')</li>'; } } |
As a super-cool extra I got to use a PHP function I had never gotten to use before, let alone knew existed: bool shuffle ( array &array ). It, you guessed it, shuffles up an array.
Posted February 5th, 2007 - PermalinkI just scratched an itch I’ve had for a long time. I always hate how on my category pages and my archive pages the little “More Posts” box would display even if there were no posts. Also, I could never check and see if there were no “Newer” or no “Older” posts so I could still print the words out in non-link form.
I finally did something about it today. I started by looking through the codex, and finding nothing. So I started digging through the WP code looking for the posts_nav_link() which sorta did what I wanted, but just echoed to the page instead of returning. I found it in wp-includes/template-functions-links.php and started chopping it up.
In the end I came up with three functions for my ‘my-hacks.php’ file. I didn’t want to spend the time making it into a plugin since it’s not that big a deal. They aren’t terribly efficient, but they work and thats what matters. Also, I pre-pended jh to the function names so there wouldn’t be any chance of collisions.
Returns true if there are more posts, newer or older.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | function jh_more_posts() { global $paged, $result, $request, $posts_per_page, $wpdb, $max_num_pages; if ( isset($max_num_pages) ) { $max_page = $max_num_pages; } else { preg_match('#FROM\s(.*)\sGROUP BY#siU', $request, $matches); $fromwhere = $matches[1]; $numposts = $wpdb->get_var("SELECT COUNT(DISTINCT ID) FROM $fromwhere"); $max_page = $max_num_pages = ceil($numposts / $posts_per_page); } if( !$paged ) $paged = 1; if($paged == 1 && $paged == $max_page) return false; else return true; } |
Returns true if there are more ‘older’ posts.
1 2 3 4 5 6 7 | function jh_more_old() { global $paged; if( !$paged || $paged == 1) return true; else return false; } |
Returns true if there are more ‘newer’ posts.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | function jh_more_new() { global $paged, $result, $request, $posts_per_page, $wpdb, $max_num_pages; if ( isset($max_num_pages) ) { $max_page = $max_num_pages; } else { preg_match('#FROM\s(.*)\sGROUP BY#siU', $request, $matches); $fromwhere = $matches[1]; $numposts = $wpdb->get_var("SELECT COUNT(DISTINCT ID) FROM $fromwhere"); $max_page = $max_num_pages = ceil($numposts / $posts_per_page); } if( !$paged ) $paged = 1; if($paged == $max_page) return true; else return false; } |
While working on a “secret” project I needed a simple BBCode to HTML converter. I’m sure there are a number of them out there, but I wanted to build my own. Here it is, a quick and dirty regex filled function.
1 2 3 4 5 6 7 8 9 10 | function bbc2html($content) { $content = preg_replace('/(\[b\])(.*?)(\[\/b\])/','<strong>$2</strong>',$content); $content = preg_replace('/(\[i\])(.*?)(\[\/i\])/','<em>$2</em>',$content); $content = preg_replace('/(\[u\])(.*?)(\[\/u\])/','<u>$2</u>',$content); $content = preg_replace('/(\[ul\])(.*?)(\[\/ul\])/','<ul>$2</ul>',$content); $content = preg_replace('/(\[li\])(.*?)(\[\/li\])/','<li>$2</li>',$content); $content = preg_replace('/(\[url=)(.*?)(\])(.*?)(\[\/url\])/','<a href="$2" target="_blank">$4</a>',$content); $content = preg_replace('/(\[url\])(.*?)(\[\/url\])/','<a href="$2" target="_blank">$2</a>',$content); return $content; } |
In other news this is post #99 since I started the blog. Here’s to hoping #100 is stellar.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | function bbc2html($content) { $search = array ( '/(\[b\])(.*?)(\[\/b\])/', '/(\[i\])(.*?)(\[\/i\])/', '/(\[u\])(.*?)(\[\/u\])/', '/(\[ul\])(.*?)(\[\/ul\])/', '/(\[li\])(.*?)(\[\/li\])/', '/(\[url=)(.*?)(\])(.*?)(\[\/url\])/', '/(\[url\])(.*?)(\[\/url\])/' ); $replace = array ( '<strong>$2</strong>', '<em>$2</em>', '<u>$2</u>', '<ul>$2</ul>', '<li>$2</li>', '<a href="$2" target="_blank">$4</a>', '<a href="$2" target="_blank">$2</a>' ); return preg_replace($search, $replace, $content); } |
This is something I’ve needed to do occasionally and always have to look up. The one I like best is located at this post but I’m going to repeat it here for posterity and so I can find it again easier.
perl -pi -w -e 's/search/replace/g;' *.php
So easy, thats why Perl rocks. Thank you Bob Fulkerson for introducing us.
Other options include the frickin sweet looking ruby app SearchAndReplaceGlobal by the same guy who wrote the editor I first learned to do HTML with, Arachnophilia. I say sweet looking because I never actually got it running. I didn’t try very hard though.
Posted January 21st, 2007 - Permalink