Interwebs Vs. Justin Bieber

Poor little Justin Bieber, those nasty people from the Interwebs just won’t stop circulating baseless and derogatory rumors about the 16 year old ‘Baby’ star on an almost daily basis.

So far the list of rumours, ranging from the absurd to the downright obscene, include getting arrested for swearing at the Police (6 June), dying – for the 3rd time (12 June), acquiring syphilis from a hooker (14 June), breaking his neck (15 June), bribing Chris Johnson to cover up the syphilis (16 June), really being only 12 years old (July 4), making a sex tape with Kim Kardashian (18 June), becoming pregnant (21 June), his mother posing topless in Playboy (24 June), being a hermaphrodite (25 June), and having his left testicle removed last week! It’s a wonder he finds time to sing!


String abstract PHP function

As a web developer, I’m often trying to squeeze text into small spaces, and sometimes the only way is to create a summary, or abstract of the text.

A lot of abstract functions will find the first n words:

<?php
preg_match('/^([^.!?\s]*[\.!?\s]+){0,n}/', strip_tags($text), $abstract);
echo $abstract[0];
?>

… or the first n sentences:

<?php
preg_match('/^([^.!?]*[\.!?]+){0,n}/', strip_tags($text), $abstract);
echo $abstract[0];
?>

… but this isn’t always enough. Three sentences, (or 50 words for that matter), could run anywhere between 150 and 450 characters long, which is a big variation if you only have room to display 180 characters! So here’s my reusable abstract function that will…

… truncate after the last word that appears within your specified character limit:

<?php
function abstract( $string, $len ) {
  if ( strlen($string) > $len ) {
    $pos = ( $len - stripos(strrev(substr($string, 0, $len)), ' ') );
    $sum = substr($string, 0, $pos-1);
    $chr = $sum[strlen($sum)-1];
    if ( strpos($chr, '.,!?;:') ) {
       // STRIP LAST CHAR
       $sum = substr($sum, 0, strlen($sum)-1);
    }
    // ADD ELLIPSIS
    return $sum."&#8230;";
  } else {
    return $string;
  }
}
echo abstract( $string, 180 );
?>


I Hate the French (lyrics)

Extracts from “I Hate the French”, a satirical comedy song written by Richard Curtis and performed by Howard Goodall, live during Rowan Atkinson’s 1980 UK tour.

They all wear berets and they’re all called “Jacques”,
They even steal from us the words they lack:
“le Weekend”, “le Camping” and “Cul-de-sac”,
That’s why I hate the French…

They bake their bread in such a naughty shape,
They brag about their wine, and worship the grape,
They criticise our food but then they eat “Crêpe”!
That’s why I hate the French…

And now they’ve started coming here in droves,
“French cigarettes”, “French letters”, and “French clothes”.

I’m sick and tired of eating all this “Brie”,
And I’ll be buggered if I go to “Gay Paris”!

They’re pretty cocky ’bout their “games in the dark”,
They think with girls they light a “special spark”,
But look what the bastards did to Joan of Arc!
That’s why I hate the French…


Really useful RegEx bits

If like me, you often need to use regular expressions, but still after over 10 years of using them can waste an entire morning just trying to write a simple search and replace, here are some really useful snippets I have used along the way.

To match an HTML link within a string, and to isolate the inner HTML, (whatever occurs between the opening and closing anchor tags), use:

<?php
preg_match('#<a\s+.*?href=[\'"]([^\'"]+)[\'"]\s*
    (?:title=[\'"]([^\'"]+)[\'"])?.*?>((?:(?!</a>).)*)</a>#i', $input, $match);

// $input - A STRING CONTAINING AN ANCHOR LINK WITH INNER HTML
$input = 'This is some text containing a linked image <a
    href="http://www.google.com"><img src="google.jpg" alt="Google"
    width="200" height="50" /></a>. Goodbye.';

// $match[0] RETURNS THE ENTIRE LINK INCLUDING THE INNER HTML AND ANCHOR TAGS
$match[0] == '<a href="http://www.google.com"><img src="google.jpg"
    alt="Google" width="200" height="50" /></a>';

// $match[1] RETURNS JUST THE ANCHOR LINK
$match[1] == 'http://www.google.com';

// $match[2] RETURNS THE INNER HTML, IN THIS CASE, JUST THE IMAGE TAG
$match[2] == '<img src="google.jpg" alt="Google" width="200" height="50" />';
?>

To match an email address, and to isolate either the username or domain name part of the address, use:

<?php
preg_match('#([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i',
    $input, $match);

// $input - A STRING CONTAINING AN EMAIL ADDRESS
$input = 'My email address is spam-a-lot@mydomain.com. Write me!';

// $match[0] RETURNS ONLY THE VALID EMAIL ADDRESS
$match[0] == 'spam-a-lot@mydomain.com';

// $match[1] RETURNS ONLY THE USERNAME PART
$match[1] == 'spam-a-lot';

// $match[2] RETURNS ONLY THE DOMAIN NAME PART
$match[2] == 'mydomain.com';
?>

To match an email address, and replace it with a clickable link to the email address, use:

<?php
$ret = preg_replace("#([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i",
    "\\1<a href="mailto:\\2@\\3">\\2@\\3</a>", $input);

// $input - A STRING CONTAINING AN EMAIL ADDRESS
$input = 'My email address is spam-a-lot@mydomain.com. Write me!';

// $ret - RETURNED STRING CONTAINING A CLICKABLE EMAIL LINK
$ret == 'My email address is <a
    href="mailto:spam-a-lot@mydomain.com">spam-a-lot@mydomain.com</a>.
    Write me!';
?>

1 Comment more...

Can you say Eyjafjallajokull?


Working with MySQL databases from PHP

If like me, you often forget what your database tables are called, or need to check if a MySQL table has been created successfully, but don’t have access to a handy control panel or interface, you may need to write a small PHP script to fetch the results you need. Here are a few that I find myself using time and time again:

Connect to your MySQL database

<?php
$db_user = 'joe_bloggs';
$db_pass = 'test123';
$db_host = 'localhost';
$db_name = 'my_database';

$db = mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name , $db);
?>

Get a list of all tables in a database

<?php
$query = mysql_query("SHOW TABLES");
while ($result = mysql_fetch_array($query)) {
  echo $result[0] . "<br>";
}
?>

Get a list of all data in a table

<?php
$db_table = "my_table";
$query = mysql_query("SELECT * FROM '$db_table'");
while ($row = mysql_fetch_assoc($query)) {
  print_r($row) . "<br>";
}
?>


Flight Of The Conchords… Mutha Ucker!

“Flight Of The Conchords” are New Zealand comedy duo Bret McKenzie and Jemaine Clement, and sell themselves as “formerly New Zealand’s fourth most popular guitar-based digi-bongo acapella-rap-funk-comedy folk duo”. Their combination of witty observation, characterisation and acoustic guitar music has gained them a worldwide cult following. If you haven’t heard of them already, check them out.

Here are some of their funnier YouTube clips:


New iPhone 4G prototype left in bar!

New iPhone 4G prototype

On the evening of Thursday, 18th of March, Apple Software Engineer Gray Powell was celebrating his 27th birthday at Gourmet Haus Staudt, a German beer garden just 20 miles from the company’s Infinite Loop headquarters in Redwood City, California.

He also happened to be field-testing the next-generation iPhone at the time. He posted a Facebook update from the secret prototype, (disguised as a regular 3G), “I underestimated how good German beer is“.

Obviously, he underestimated too much, as he promptly set the new iPhone down on the stool next to him and left the bar! Gizmodo got the phone and full story here:
http://gizmodo.com/5520164/this-is-apples-next-iphone

Rumour is, Bill Gates also left a new Microsoft prototype phone in the bar, but three weeks later it was still there…


The Apple iPad – unveiled!

The Apple iPad

Apple is, at this very moment, unveiling the new iPad!

An A4 size version of the iPhone, 0.5 inches thick, 1.5 pounds, 9.7″ touch screen and a 1GHz chip. It comes in 16GB, 32GB and 64GB. WiFi and Bluetooth, an accelerometer, compass, speaker, microphone and the standard iPod connector, 10 hours battery life and a month of standby time.

Oh, and I want one!


Demotivational: Geek Humour

“Motivational Posters” are the kind commonly found in schools and corporate offices, designed to inspire people to achieve more or think differently. Over the years, they have been repeatedly parodied, and “Demotivational Posters” have become an Internet meme, juxtaposing banal optimism with comical pessimism. These are some of my favourites:

Geek Humour – Because no one else gets your jokes anyway.


Meta

Site Traffic

Total visitors23,928
Visitors today106
On best day387
Online now6
Most online25
Our top referrering sites
Lettingfactory.co.uk 16
Conveyancingshoppers.co.u 10
Legalsoundz.com 7
Chic-chic-store.com 6
Officefurnitureetc.com.au 4
Wh-tech.com 3
Freehosting-site.com 3
Allmedicalquestion.com 3
Squidoo.com 3
Gtcbay.com 3
Copyright © 1996-2010 Neil Hillman. All rights reserved.
iDream theme by Templates Next | Powered by WordPress