Web Development

UNHCR graphic depicting Camp Ashraf

Camp New Iraq (formerly Camp Ashraf)

This morning I was tasked, at short notice, by the UN Refugee Agency to create a small graphic to accompany a press release about Camp Ashraf, (now being called “Camp New Iraq“).

With little information to go on, I decided on a simplified map of Iraq, depicting the location of Camp Ashraf, and it’s proximity to Baghdad. I produced this graphic in under an hour, and I was quite pleased with the outcome.

Some background on former Camp Ashraf

Ashraf is a city turned 25-year-old refugee camp, just 40 miles north of Baghdad. It’s residents, Iranian militant opposition group the People’s Mujahedin of Iran (PMOI), were exiled when they fought alongside Iraqis soldiers against Iran’s cleric leaders in the First Persian Gulf War.

Despised in their own country as traitors, and with little sympathy remaining in post-Saddam Iraq, the fate of the residents of Ashraf now hangs in the balance. The risks of violence are high, and come from both sides, with frequent clashes there resulting in loss of lives.


Available pronounceable 5 letter domains: A-D

As you are probably aware, there are no longer any 4 letter .com domain names available, and all 8,938 English five letter words have already been registered. So if you want a short, memorable 5 letter domain name, your best bet is probably to start searching among the pronouncable 5 letter non-words.

I have prepared a list here of over 900 available pronounceable 5 letter domain names starting with A, B, C and D, (excluding those containing the undesirable characters such as J,K,Q,U,V,W,X,Y,Z). There are some good names here, like Afigi.com, Begog.com, Bigef.com and Cobof.com, to highlight a few examples.

If you decide to register one, please leave a comment, as these list took me several hours to produce. If I get enough feedback, I will carry on through the alphabet… ;-)

153 available pronounceable 5 letter domains starting with ‘A’

abahi.com   abefa.com   abefe.com   abefo.com   abehe.com   abehi.com
abife.com   abifo.com   abihi.com   abofo.com   abohe.com   aceho.com
acife.com   aciha.com   acihe.com   acihi.com   adehi.com   adogi.com
afafo.com   afahi.com   afane.com   afebi.com   afebo.com   afefo.com
afegi.com   afeha.com   afehe.com   afehi.com   afeho.com   afene.com
afigi.com   afihe.com   afihi.com   afiho.com   afoba.com   afobi.com
afoce.com   afoci.com   afodi.com   afofe.com   afofi.com   afoge.com
afogi.com   afoha.com   afohe.com   afoho.com   afole.com   afose.com
afote.com   agefe.com   agifo.com   agiha.com   agihe.com   agihi.com
agiho.com   agoce.com   agofe.com   agohe.com   agohi.com   ahace.com
ahafi.com   ahahe.com   ahebi.com   ahece.com   aheci.com   ahede.com
ahefe.com   ahefo.com   ahega.com   ahege.com   ahegi.com   ahehi.com
aheno.com   ahesi.com   ahete.com   aheti.com   ahibi.com   ahifa.com
ahife.com   ahifo.com   ahigi.com   ahobi.com	ahoce.com   ahoci.com
ahodi.com   ahofa.com   ahofe.com   ahofi.com   ahofo.com   ahogi.com
ahohe.com   ahohi.com   ahono.com   alafe.com   allfe.com   allih.com
amehe.com   ammab.com   ammaf.com   ammbe.com   ammce.com   ammdo.com
ammfe.com   ammfi.com   ammfo.com   ammid.com   ammif.com   ammla.com
ammli.com   ammlo.com   ammme.com   ammmi.com   ammri.com   ammso.com
ammti.com   ammto.com   anefe.com   anehe.com   anehi.com   anihe.com
anihi.com   anofe.com   anofo.com   anohe.com   arehe.com   arehi.com
areho.com   areri.com   arihi.com   aroci.com   arofi.com   asebe.com
asehe.com   aseho.com   asifo.com   asihe.com   atehi.com   athac.com
athaf.com   athag.com   athef.com   atheg.com   atheh.com   athib.com
athig.com   athih.com   athob.com   athof.com   athoh.com   atihe.com
atihi.com   atoce.com   atofe.com

241 available pronounceable 5 letter domains starting with ‘B’

babef.com   babof.com   baceb.com   baceg.com   baceh.com   bacif.com
bacih.com   bacof.com   badef.com   badeg.com   badof.com   bafaf.com
bafeb.com   bafec.com   bafeg.com   bafeh.com   bafib.com   bafif.com
bafig.com   bafih.com   bafob.com   bafof.com   bafoh.com   bafot.com
bagaf.com   bagef.com   bageg.com   bagib.com   bagif.com   bagih.com
bahaf.com   bahec.com   bahed.com   bahef.com   bahib.com   bahif.com
bahig.com   bahih.com   bahob.com   bahod.com   bahof.com   bahog.com
bahoh.com   baleb.com   balef.com   balob.com   bamaf.com   bameb.com
bameg.com   bamib.com   bamof.com   banih.com   banoh.com   basib.com
bateb.com   batef.com   batof.com   bebaf.com   bebah.com   bebif.com
bebih.com   bebof.com   becec.com   becef.com   beceg.com   becib.com
becif.com   becih.com   becoc.com   bedib.com   bedif.com   bedoh.com
befeb.com   befeg.com   befeh.com   befib.com   befif.com   befih.com
befob.com   befof.com   befoh.com   befot.com   begac.com   begah.com
begeh.com   begib.com   begih.com   begof.com   begog.com   begoh.com
behah.com   beheb.com   behef.com   beheg.com   behif.com   behig.com
behih.com   behog.com   behoh.com   bemaf.com   bemah.com   bemef.com
bemeg.com   bemib.com   bemif.com   bemih.com   bemoh.com   benob.com
benof.com   berab.com   berib.com   berif.com   berih.com   berob.com
beroh.com   besah.com   besif.com   bibaf.com   bibef.com   bibeg.com
bibof.com   bicag.com   bicah.com   bicef.com   biceg.com   biceh.com
bicif.com   bicih.com   bicog.com   bideb.com   bidih.com   bifad.com
bifaf.com   bifah.com   bifec.com   bifef.com   bifeg.com   bifeh.com
bifib.com   bifig.com   bifih.com   bifir.com   bifob.com   bifof.com
bifoh.com   bifom.com   bigef.com   bigih.com   bihaf.com   biheb.com
bihed.com   bihef.com   biheg.com   biheh.com   bihic.com   bihid.com
bihif.com   bihig.com   bihih.com   bihil.com   bihir.com   bihob.com
bihoc.com   bihod.com   bihof.com   bihog.com   bihol.com   bilaf.com
bilef.com   biloh.com   bimeb.com   bimif.com   bimih.com   bimim.com
bimof.com   bimog.com   bimoh.com   bimom.com   binaf.com   binib.com
binif.com   binih.com   binof.com   binoh.com   birif.com   birig.com
birir.com   birob.com   bisif.com   bisih.com   bisob.com   bitih.com
bitob.com   bobeg.com   bobif.com   bobih.com   bodif.com   bofeb.com
bofef.com   bofeg.com   bofib.com   bofif.com   bofig.com   bofih.com
bofoc.com   bofof.com   bofoh.com   bogeb.com   bogef.com   bogeg.com
bogeh.com   bogib.com   bogih.com   bohaf.com   boheb.com   bohed.com
bohib.com   bohid.com   bohif.com   bohih.com   boleb.com   bolib.com
bolif.com   bomaf.com   bomef.com   bomeh.com   bomib.com   bomih.com
bomoc.com   boneg.com   bonih.com   bonof.com   borif.com   borih.com
borof.com   bosaf.com   boseb.com   bosib.com   bosof.com   botef.com
botif.com

261 available pronounceable 5 letter domains starting with ‘C’

cabaf.com   cabeh.com   cabif.com   cabih.com   cabof.com   cacaf.com
caceb.com   caceg.com   cacih.com   cacog.com   cadib.com   cafaf.com
cafih.com   cafof.com   cafoh.com   cagab.com   cagag.com   cagah.com
cageb.com   cagef.com   cagig.com   cagoc.com   cagof.com   cagog.com
cagoh.com   cahaf.com   cahag.com   cahah.com   caheb.com   cahef.com
caheg.com   cahib.com   cahif.com   cahig.com   cahih.com   cahoc.com
cahod.com   cahoh.com   cahol.com   calih.com   camih.com   camog.com
canih.com   canoh.com   casof.com   casoh.com   cateh.com   catih.com
cebab.com   cebah.com   cebef.com   cebib.com   cebif.com   ceboc.com
cebod.com   cebof.com   cebog.com   ceboh.com   cecag.com   ceceb.com
cecog.com   cedeg.com   cedih.com   cedob.com   cefaf.com   cefah.com
cefeb.com   cefef.com   cefeh.com   cefih.com   cefob.com   cefof.com
cefoh.com   cegad.com   cegeg.com   cegeh.com   cegib.com   cegif.com
cegig.com   cegih.com   cegob.com   cegof.com   cegog.com   cegoh.com
cehac.com   cehad.com   cehaf.com   cehah.com   ceheb.com   cehed.com
cehef.com   ceheg.com   cehib.com   cehif.com   cehig.com   cehih.com
cehim.com   cehin.com   cehir.com   cehob.com   cehoc.com   cehod.com
cehog.com   cehoh.com   celih.com   cemeh.com   cemof.com   cemog.com
cemoh.com   ceneh.com   cenif.com   cenih.com   cenoh.com   cerab.com
cereh.com   ceroh.com   ceseg.com   cesih.com   cesob.com   cesot.com
cetah.com   cetob.com   cetog.com   cetoh.com   cibab.com   cibeb.com
cibib.com   cibif.com   ciboc.com   cibof.com   cibog.com   cibom.com
ciceb.com   ciceh.com   cideh.com   cidid.com   cidih.com   cidod.com
cidof.com   cidoh.com   cifag.com   cifeb.com   cifef.com   cifeh.com
cifet.com   cifib.com   cifim.com   cifof.com   cifog.com   cifoh.com
cigab.com   cigag.com   cigeb.com   ciged.com   cigeh.com   cigib.com
cigid.com   cigif.com   cigig.com   cigih.com   cigim.com   cigob.com
cigof.com   cigog.com   cigoh.com   cigom.com   cihab.com   cihag.com
cihah.com   ciham.com   ciheb.com   cihec.com   cihed.com   ciheg.com
ciheh.com   cihib.com   cihid.com   cihif.com   cihig.com   cihih.com
cihim.com   cihin.com   cihit.com   cihob.com   cihod.com   cihof.com
cihog.com   cihoh.com   cihol.com   cihom.com   cileh.com   cilig.com
cilll.com   cilob.com   cilod.com   cilof.com   ciloh.com   cimeh.com
cimib.com   cimig.com   cimih.com   cimog.com   cimoh.com   cinib.com
cinih.com   cinob.com   cirif.com   cirof.com   ciseh.com   cisib.com
cisih.com   cisob.com   cisod.com   cisoh.com   cisot.com   ciths.com
cobeb.com   cobef.com   cobif.com   cobih.com   cobof.com   coceh.com
cocih.com   codih.com   codob.com   codof.com   cofef.com   cofeg.com
cofeh.com   cofih.com   cofof.com   cogaf.com   cogag.com   cogeg.com
cogeh.com   cogih.com   cogob.com   cogoh.com   cohaf.com   cohag.com
cohah.com   coheb.com   cohed.com   coheg.com   coheh.com   cohid.com
cohih.com   cohir.com   cohoh.com   colih.com   coneh.com   conih.com
conoh.com   coseb.com   coseh.com   cosih.com   cosoh.com   cotib.com
cotih.com   cotof.com   cotoh.com

253 available pronounceable 5 letter domains starting with ‘D’

dabaf.com   dabem.com   dabif.com   dacag.com   daceb.com   daceg.com
daceh.com   dacib.com   dacif.com   dacir.com   dacof.com   dadeb.com
dadib.com   dadob.com   dadof.com   dafec.com   dafef.com   dafeh.com
dafib.com   dafih.com   dafob.com   dafof.com   dafoh.com   dagac.com
dagaf.com   dagec.com   dagef.com   dageg.com   dageh.com   dagib.com
dagih.com   dagof.com   dahac.com   dahah.com   dahec.com   daheg.com
daheh.com   dahet.com   dahif.com   dahig.com   dahih.com   dahob.com
dahoh.com   dalaf.com   damef.com   damib.com   damig.com   damof.com
danog.com   darib.com   daseh.com   dasih.com   dasob.com   dasoc.com
dasoh.com   dathi.com   datob.com   datof.com   debaf.com   debeh.com
debif.com   debof.com   deceb.com   deceg.com   decif.com   decih.com
dedaf.com   dedib.com   dedif.com   dedih.com   defah.com   defeb.com
defeg.com   defeh.com   defif.com   defob.com   defod.com   defoh.com
degab.com   degad.com   degah.com   degeb.com   degef.com   degib.com
degih.com   degoc.com   degof.com   degoh.com   dehad.com   deheg.com
dehid.com   dehif.com   dehih.com   dehob.com   dehoc.com   dehog.com
dehoh.com   delef.com   deloh.com   demef.com   demeh.com   demif.com
demih.com   denaf.com   desaf.com   deseh.com   desoh.com   deteh.com
detid.com   detof.com   detog.com   detoh.com   dibeb.com   dibif.com
dibig.com   dibih.com   dibof.com   dicef.com   diceg.com   diceh.com
dicig.com   dicih.com   dicof.com   dicoh.com   didaf.com   dideb.com
dideg.com   didib.com   didof.com   difad.com   difeb.com   difef.com
difeg.com   difeh.com   difet.com   difib.com   difif.com   difih.com
difoc.com   difod.com   difof.com   difoh.com   digef.com   digeg.com
dihab.com   dihaf.com   dihah.com   diheb.com   dihec.com   dihef.com
diheg.com   diheh.com   dihib.com   dihic.com   dihid.com   dihif.com
dihig.com   dihih.com   dihil.com   dihim.com   dihin.com   dihir.com
dihob.com   dihod.com   dihof.com   dihog.com   dihoh.com   dilaf.com
dileh.com   dilif.com   dilof.com   diloh.com   dimif.com   dimof.com
dimoh.com   dinif.com   dinih.com   dinob.com   direh.com   dirih.com
diroh.com   disif.com   disod.com   disoh.com   ditaf.com   diteb.com
ditif.com   ditih.com   ditof.com   dobaf.com   dobeb.com   dobef.com
dobeg.com   dobeh.com   dobif.com   dobof.com   doboh.com   docah.com
docef.com   doceg.com   doceh.com   docif.com   docig.com   docih.com
dodib.com   dofaf.com   dofag.com   dofah.com   dofeb.com   dofef.com
dofeg.com   dofeh.com   dofib.com   dofid.com   dofif.com   dofih.com
dofoc.com   dofoh.com   dogaf.com   dogec.com   dogef.com   dogeh.com
dogih.com   dohaf.com   dohag.com   dohah.com   doheb.com   dohed.com
dohef.com   doheg.com   doheh.com   dohel.com   dohib.com   dohic.com
dohih.com   dohir.com   dohof.com   dohoh.com   dolef.com   dolih.com
dolof.com   doloh.com   domef.com   domih.com   donif.com   donih.com
dosaf.com   dosah.com   doseh.com   dosif.com   dosih.com   dosoh.com
dotih.com
1 Comment more...

Quick Social Media buttons in a jQuery function

Social Media share buttons

We’ve all seen them, the Tweet this, Like this, Share this social media buttons. They give end users an easy way to share content, whilst providing webmasters with an important promotional service for their sites.

But adding them to your sites can be a bit of a pain. You have to visit a seperate generator page for each social network button, (Twitter buttons, Facebook Like buttons and Google +1 buttons), and fill in all the same details on each one, (your site URL, page URL, text). You then have to cut and paste the three different Javascript snippets into your source code, and if you want to do this on multiple pages, you have to repeat the process to generate unique Javascript code snippets for each URL, which can be incredibly tedious and time-consuming.

This is why I wrote a jQuery function that enables you to quickly and easily add these three buttons to all your pages. It employs the lesser-known iframe method for displaying the buttons, and can be added site-wide to your templates or scripts by including a jQuery function and a couple of lines of code.

Firstly, include your copy of the jQuery library in your document’s header:

<script src="/js/jquery.min.js" type="text/javascript"></script>

… or better still, link to the copy in Google AJAX Libraries, (as using Google’s network of datacenters instead of hosting locally will decrease latency, increase parallelism and improve caching on your site!):

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"
    type="text/javascript"></script>

Secondly, include the following jQuery function, either in your document’s header, or in a linked external javascript file. You don’t need to change anything in this script.

<script type="text/javascript">
function social_media_buttons(site,text,page) {
    // First, check if 'social-media-buttons' div exists...
    if ($('#social-media-buttons').length) {

        // Create Twitter button
        $('#social-media-buttons').append('<div id="twitter-button"></div>');
        $('#twitter-button').attr("style","float: left;");
        $('#twitter-button').append('<iframe id="share-twitter"></iframe>');
        var twitter = $('iframe#share-twitter');
        twitter.attr("title", "Twitter Tweet Button");
        twitter.attr("frameBorder", "0");
        twitter.attr("scrolling", "no");
        twitter.attr("marginWidth", "0");
        twitter.attr("marginHeight", "0");
        twitter.attr("allowtransparency", "true");
        twitter.attr("vspace", "0");
        twitter.attr("hspace", "0");
        twitter.attr("tabindex", "-1");
        twitter.attr("style","width: 55px; height: 62px;");
        var src = 'http://platform.twitter.com/widgets/tweet_button.html?count=vertical';
        src = src + '&enableNewSizing=false&lang=en&original_referer=' + site;
        src = src + '&size=m&text=' + encodeURI(text) + '&url=' + page;
        twitter.attr("src", src);

        // Create Google button
        $('#social-media-buttons').append('<div id="google-button"></div>');
        $('#google-button').attr("style","float: left; margin: 2px 8px;");
        $('#google-button').append('<iframe id="share-google"></iframe>');
        var google = $('iframe#share-google');
        google.attr("title", "Google +1 Button");
        google.attr("frameBorder", "0");
        google.attr("scrolling", "no");
        google.attr("marginWidth", "0");
        google.attr("marginHeight", "0");
        google.attr("allowtransparency", "true");
        google.attr("vspace", "0");
        google.attr("hspace", "0");
        google.attr("tabindex", "-1");
        google.attr("style","width: 50px; height: 62px;");
        var src = 'https://plusone.google.com/_/+1/fastbutton?url=' + page;
        src = src + '&useSharedProxy=true&rcache=true&scache=true&size=tall';
        src = src + '&count=true&hl=en-US&parent=' + site;
        google.attr("src", src);

        // Create Facebook button
        $('#social-media-buttons').append('<div id="facebook-button"></div>');
        $('#facebook-button').attr("style","float: left;");
        $('#facebook-button').append('<iframe id="share-facebook"></iframe>');
        var facebook = $('iframe#share-facebook');
        facebook.attr("title", "Facebook Like Button");
        facebook.attr("frameBorder", "0");
        facebook.attr("scrolling", "no");
        facebook.attr("marginWidth", "0");
        facebook.attr("marginHeight", "0");
        facebook.attr("allowtransparency", "true");
        facebook.attr("vspace", "0");
        facebook.attr("hspace", "0");
        facebook.attr("tabindex", "-1");
        facebook.attr("style","width: 60px; height: 62px;");
        var src = 'http://www.facebook.com/plugins/like.php?action=like&href=' + page;
        src = src + '&layout=box_count&show_faces=false&width=450&colorscheme=light';
        facebook.attr("src", src);
    }
}
</script>

Thirdly, on document ready, define your variables and call the function, (place this within the document’s header):

<script type="text/javascript">
$(document).ready(function() {

    var site = "http://www.yoursite.com/";
    var text = "Tell everyone about how great your page is!";

    // This is the url of the page you want to share
    // You can either populate this manually,
    // or let Javascript use the current page value:
    // var page = "http://www.yoursite.com/path/to/page.html";
    var page = window.location.protocol + "://" + window.location.host
             + window.location.pathname;

    social_media_buttons(site,text,page);
});
</script>

Finally, place the buttons where you want them to appear, by adding a <div> with the ID “social-media-buttons” anywhere on your page, like so:

<div id="social-media-buttons"></div>

That’s it, social media buttons! Let me know how you get on…
 
Social Media share buttons


Resize JPEG images using PHP

This is something I have had to do on numerous occasions, and each time I have to look up GD library functions and mess around with several attempts before I get it right, so I figured it was time I wrote a couple of simple PHP functions to handle this. Hopefully you can use them too…

Resize a JPEG to a maximum width and height whilst maintaining the aspect ratio (proportions).

Resize a JPEG whilst maintaining aspect ratio (proportions)

<?php
// LOCATION OF ORIGINAL IMAGE
$orig = "path/to/original_image.jpg";

// DESTINATION OF RESIZED IMAGE
$dest = str_replace(".jpg","_resized.jpg",$orig);

// MAXIMUM NEW WIDTH & HEIGHT (depending on original's aspect ratio)
$w = 200;
$h = 200;

// RUN FUNCTION
ResizeTo($orig,$dest,$w,$h);

// FUNCTION
function ResizeTo($orig,$dest,$h,$w) {
	// GET WIDTH AND HEIGHT OF ORIGINAL
	list($width, $height) = getimagesize($orig);

	// CALCULATE NEW WIDTH AND HEIGHT
	if ($width > $height) {

		// LANDSCAPE
		$new_width = $w;
		$new_height = round($height * ($w / $width));
		if ($new_height > $h) {
			$new_height = $h;
			$new_width = round($width * ($h / $height));
		}

	} else {

		// PORTRAIT
		$new_height = $h;
		$new_width = round($width * ($h / $height));
		if ($new_width > $w) {
			$new_width = $w;
			$new_height = round($height * ($w / $width));
		}
	}

	// OPEN IMAGES IN MEMORY
	$source = imagecreatefromjpeg($orig);
	$thumb = imagecreatetruecolor($new_width,$new_height);

	// RESIZE
	imagecopyresized($thumb, $source, 0, 0, 0, 0,
		$new_width, $new_height, $width, $height);

	// SAVE RESIZED
	imagejpeg($thumb, $dest, 90);

	// DESTROY IMAGES IN MEMORY
	imagedestroy($source);
	imagedestroy($thumb);
}
?>

Sometimes you don’t want to maintain your image’s aspect ratio, you want to resize and crop all images to the same height and width, and for that I created a second function:

Resize and crop a JPEG to a specific width and height

Resize and crop a JPEG to a specific size

<?php
// LOCATION OF ORIGINAL IMAGE
$orig = "path/to/original_image.jpg";

// DESTINATION OF RESIZED IMAGE
$dest = str_replace(".jpg","_croped.jpg",$orig);

// MAXIMUM NEW WIDTH & HEIGHT (depending on original's aspect ratio)
$w = 200;
$h = 200;

// RUN FUNCTION
ResizeCropTo($orig,$dest,$w,$h);

// FUNCTION
function ResizeCropTo($orig,$dest,$h,$w) {

	// GET WIDTH AND HEIGHT OF ORIGINAL
	list($width, $height) = getimagesize($orig);

	// CALCULATE NEW OFFSET POSITION
	if ($width > $height) {

		// LANDSCAPE
		$new_height = $h;
		$new_width = round($width * ($h / $height));
		$y_offset = 0;
		// POSITION IN THE CENTER FOR LANDSCAPE
		$x_offset = ($new_width - $w) / 2);
		if ($x_offset < 0) {
			$new_width = $w;
			$new_height = round($height * ($w / $width));
			$x_offset = 0;
			// WE ERR MORE TO THE TOP ON PORTRAIT
			$y_offset = ($new_height - $h) / 4);
		}

	} else {

		// PORTRAIT
		$new_width = $w;
		$new_height = round($height * ($w / $width));
		$x_offset = 0;
		// WE ERR MORE TO THE TOP ON PORTRAIT
		$y_offset = ($new_height - $h) / 4);
		if ($y_offset < 0) {
			$new_height = $h;
			$new_width = round($width * ($h / $height));
			$y_offset = 0;
			// POSITION IN THE CENTER FOR LANDSCAPE
			$x_offset = ($new_width - $w) / 2); 		}
	}

	// OPEN IMAGES IN MEMORY
	$source = imagecreatefromjpeg($orig);
	$thumb = imagecreatetruecolor($h,$w);

	// RESIZE AND CROP
	imagecopyresized($thumb, $source, $x_offset, $y_offset, 0, 0,
		$new_width, $new_height, $width, $height);

	// SAVE RESIZED
	imagejpeg($thumb, $dest, 90);

	// DESTROY IMAGES IN MEMORY
	imagedestroy($source);
	imagedestroy($thumb);
}
?>


Creepy facial animation girl!

New and unsettling facial animation technology that creates a variety of expressions from a single image… developed by those clever Japanese guys at MotionPortrait.
Move your mouse around her head…


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 truncate after n words:

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

… or truncate after 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 );
?>


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!';
?>


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>";
}
?>


Copyright © 1996-2010 Neil Hillman. All rights reserved.
iDream theme by Templates Next | Powered by WordPress