Geocode from Google API V3

If you want to get the latitude and longitude from an address using PHP then try the following code snippet:

$geoResult = file_get_contents('' . urlencode($addressString) . '&sensor=false&key=API');
if ($geoResult !== FALSE) {
$output= json_decode($geoResult);
@$lat = $output->results[0]->geometry->location->lat;
@$long = $output->results[0]->geometry->location->lng;
if ($output->status == 'OK') {
if ($lat > NULL) {
//do something

PHP: Write to a file

The following code writes to a file, it will overwrite the current contents of the file:

$string = "file contents goes here";
$myFile = "file.php";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $string);

Make sure you give file.php write access on the server (Apache chmod 775 normally works).

PHP: Limit a strings characters

This function will limit the number of words shown if the total character limit is over a certain number and add … after the string. Very useful if you want to trim titles or anywhere where you are limited by space:

function limitCharacters($string){
$count = strlen($string);
// change 50 to the number of characters you want to check for
if($count > 50){
//change 5 to the number of words you want to limit to.
//change ... to anything you want to use to show its clipped
$newstring = implode(' ',array_slice(explode(' ',$string),0,5)).'...';
$newstring = $string;
return $newstring;

To call the function simply use:


Drupal CCK Autonumber Field

In a Drupal site you may have a content type that needs an auto number to be generated that auto increments on each new node of that type.

To do this install the computed field module

Then create a new field of the type computed field and add the code from the following URL

Increase the memory available to PHP

If you get out of memory errors in PHP or if Drupal suggests you increase the memory available to PHP then you can do this with the following line of code:


In Drupal the best place to put this is in the settings.php file found in sites/default/