Sep 11 2011

Horizontally and Vertically Center a DIV with CSS…

Ben Hoskins

An extremely simple solution to a surprisingly elusive problem. To perfectly center a DIV on an HTML page simply use the following approach.
Continue reading


Aug 9 2011

PHP Strings and Characters

Ben Hoskins

Here some simple tips to help with manipulating text, the simplest of things can be the most valuable. :)

First, if you only want to pull one character from a string, it’s a lot easier to use the following:

 $string = 'puppy';
 echo $string[0]; // p
 echo $string[1]; // u
 echo $string[2]; // p ...and so on

And here’s the basic concept behind the substr() function:

substr($string, start # [, length #])

– length is in brackets because it’s optional

Now let’s use this function to get a better idea of what it does.

 $string = 'categories';
 echo substr($string, 0); // 'categories' because we asked to start at the first character (which is 0 from the first example above) and goes right until end of string
 echo substr($string, 3); // 'egories' because started at the third character in the string and went to the end
 echo substr($string, -1); // 's' this is different from above because it starts at the end and goes left that number of characters
 echo substr($string, -3); // 'ies'

echo substr($string,0,-1); // 'categorie' strips last letter off
 echo substr($string,3,4); // 'egor' - starts at the fourth character (remember 0 is the first) and shows 4 characters total (length)
 echo substr($string,1,3); // 'ate' - starts at the second character and shows 3 characters total (length)
 echo substr($string,1,-2); // 'ategori' because we started at the second character and also stripped off the last two with the "-2"
 echo substr($string,-3,-2); // 'i' since we're starting from the third character from the left and then are stripping off two characters from that with the "-2"

I sometimes use this if I only want to display a preview description rather than showing the whole large description.

$text = 'Great for home, school or travel. Wonderful gift item!';
 echo substr($text,0,25).'...'; // displays "Great for home, school or..."

May 26 2011

PHP Add Key & Value to Beginning of an Array…

Ben Hoskins

So it turns out there is no built in function to add a key and value to the start of an array. There is;

 

array_unshift() - Prepend one or more elements to the beginning of an array.

 

However, this will only add a value. So the method we use is to create a new array with the key and value you wish to add to another and use;

 

array_combine — Creates an array by using one array for keys and another for its values.

 

For example:

$existingArray = array("fred" => "orange", "john" => "IsaSpy");
$array['our_key'] = "our_value";

$newArray = array_combine($existingArray, $array);

print_r($newArray);

//boom

Apr 28 2011

The Honey Pot Project…

Ben Hoskins

In tandem with my other post today (http://blog.hozzamedia.com/website-design/javascript/fend-off-the-bad-bots-ip-blocklist/).

The Honey Pot Project is a terrific organisation who build and manage helpful information about suspicious IP’s. There main thing is “The Honey Pot” – a deception trap designed to get bots, harvesters,spammers etc. to spam their software which can collect information on what they are doing and warn the community about them before they cause a problem.

Continue reading


Apr 28 2011

Fend off the bad bots…

Ben Hoskins

I have now discovered a few tips and tricks to fend off those bad bots, crawlers, harvesters, you name it.

First of all you need to create yourself a simple blocklist of IP’s, one IP per line, IP’s you dont want visiting your site. (hopefully HozzaMedia will be publishing our own one for public use soon)

123.567.678.9
657.387.93.2
1.1.1.14
etc

The PHP needs to be something simple like a script that checks if the users IP is in the text file and if it is, display a “No Entry Error”. Originally my script only displayed a HTML page with text along the lines of “Your IP address has been blocked from our network due to suspicious activity, you are being monitored. Continue reading


Apr 23 2011

Google Currency Conversion…

Ben Hoskins

Well, the api is pretty straight forward.  It was found in the Currency Conversion gadget from iGoogle by http://oohhyeah.blogspot.com/2009/01/google-currency-conversion-api.html

Here is the example:
http://www.google.com/ig/calculator?hl=en&q=100EUR%3D%3FGBP

And the resposnse looks like this:
{lhs: “100 Euros”,rhs: “191.745037 British pounds”,display: “DISPLAY_FULL_PAGE”,error: “”,icc: true}

Heres how to do it in PHP


$amount = "100";

$from_Currency = "EUR";

$googleResponce = file_get_contents('http://www.google.com/ig/calculator?hl=en&q=' . $amount . $from_Currency . '%3D%3FGBP');

$googleResponce = explode(',', $googleResponce);

$numberInGBP = number_format(round(trim(str_ireplace(array('rhs: "', ' British pounds"'), '', $googleResponce[1])), 2), 2);

echo $numberInGBP;

Nov 26 2010

PHP Link all URL’s in a string…

Ben Hoskins

Wanted all the URL’s and email addresses in a string to become actual HTML link when echoed, for our notepad software http://hmnotes.com/.

After a while of searching I came accross this…..

function makeLink($string){
//make sure there is an http:// on all URLs
$string = preg_replace("/([^\w\/])(www\.[a-z0-9\-]+\.[a-z0-9\-]+)/i", "$1http://$2",$string);

//make all URLs links
$string = preg_replace("/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i","<a target=\"_blank\" href=\"$1\">$1</a>",$string);

//make all emails hot links
$string = preg_replace("/([\w-?&;#~=\.\/]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?))/i","<a href=\"mailto:$1\">$1</a>",$string);

return $string;
}

Makes all emails, mailto: and all links as links! Happy Days!


Nov 15 2010

PHP Browser Detection…

Ben Hoskins

Detecting the user’s browser type and version is helpful in web applications that harness some of the newer bleeding edge concepts. With the browser type and version you can notify users about challenges they may experience and suggest they upgrade before using such application. Not a great idea on a large scale public site; but on a private application this type of check can be helpful.

Searching for a way to do this at the PHP layer and not at the client layer was more of a challenge than I would have guessed; the only script available was written by Gary White and Gary no longer maintains this script because of reliability. I do agree 100% with Gary about the readability; however, there are realistic reasons to desire the user’s browser and browser version and if your visitor is not echoing a false user agent we can take an educated guess.

Check out the script and find out more information on the link below

http://chrisschuld.com/projects/browser-php-detecting-a-users-browser-from-php/


Nov 3 2010

PHP preg_match and Regular Expressions…

Ben Hoskins

Regular expressions are a powerful tool for examining and modifying text. Regular expressions themselves, with a general pattern notation almost like a mini programming language, allow you to describe and parse text. They enable you to search for patterns within a string, extracting matches flexibly and precisely. However, you should note that because regular expressions are more powerful, they are also slower than the more basic string functions. You should only use regular expressions if you have a particular need.

This tutorial gives a brief overview of basic regular expression syntax and then considers the functions that PHP provides for working with regular expressions.

PHP supports two different types of regular expressions: POSIX-extended and Perl-Compatible Regular Expressions (PCRE). The PCRE functions are more powerful than the POSIX ones, and faster too, so we will concentrate on them.

Continue reading


May 6 2010

Free Stock Photos…

Ben Hoskins

As a website designer, good quality images are essential to most of my designs. There are some good stock photo websites such as iStockPhoto but the prices are over the top. In this case Google is our friend as it scans everything and if you know how to search it correctly you can find almost anything.

Most website designers, once they have paid for a stock photo image, will not rename them.

Simply type into a Google Image Search:

inurl:"istock" "your search terms"

P.S. If you do ever buy a stock photo, please do not rename it.