Coloring regions in Google Maps JavaScript API v3

December 22nd, 2011 by Alex No comments »

Want to color a map region in Google Maps V3? Easy as pie: load map, centrify on a particular point, define polygon points and options, attach polygon to map with polygon.setMap:

Loading …


<div id="color-maps" style="height: 400px">
  <div style='padding-top: 180px; text-align: center'>Loading ...</div>
</div>
 
<script type='text/javascript' src='http://maps.googleapis.com/maps/api/js?sensor=false'></script>
<script type='text/javascript'>
function colormaps() {
  // 1. setup map
  var myLatlng = new google.maps.LatLng(25, -70);
  var myOptions = {
    zoom: 4,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  var map = new google.maps.Map(document.getElementById("color-maps"), myOptions);
  // 2. define points and polygon
  var triangleCoords = [
    new google.maps.LatLng(25.774252, -80.190262),
    new google.maps.LatLng(18.466465, -66.118292),
    new google.maps.LatLng(32.321384, -64.75737),
    new google.maps.LatLng(25.774252, -80.190262)
  ];
  var bermudaTriangle = new google.maps.Polygon({
    paths: triangleCoords,
    strokeColor: "#E01B2F",
    strokeOpacity: 0.8,
    strokeWeight: 2,
    fillColor: "#E01B2F",
    fillOpacity: 0.35
  });
  // 3. attach polygon to map
  bermudaTriangle.setMap(map);
}
google.maps.event.addDomListener(window, 'load', colormaps);
</script>

MySQL regexp word boundary explained

December 22nd, 2011 by Alex No comments »

The word boundary regexp is something you may need when a list of data is saved in a single column. Example:

id   numbers
1    2,34,647
2    78,102,342
3    34,78,103

Now our task would be to select IDs where numbers contain “2″ and “34″. One would say that we can do

SELECT * FROM mytable WHERE numbers LIKE "2" OR numbers LIKE "34"

But that won’t work correctly, because 2 will match 2, 102 and 342, which is something we don’t need. In this case MySQL word boundary regex comes handy:

SELECT * FROM mytable WHERE numbers REGEXP "[[:<:]]2[[:>:]]" OR numbers REGEXP "[[:<:]]34[[:>:]]"

Take a look at [[:<:]] and [[:>:]]. This is all we need here. The word boundary regex makes sure the match we are looking for is a standalone word, matching without commas and spaces, and making sure the “2″ word doesn’t have additional letters/numbers on left/right side. This is pretty awesome and saves our day!

Disable Skype from converting phone numbers

December 14th, 2011 by Alex No comments »

Another annoying bug – another simple fix:

<meta name="SKYPE_TOOLBAR" content="SKYPE_TOOLBAR_PARSER_COMPATIBLE" />

I wonder why they don’t make it “disabled” by default. Someone also suggested to put a hidden underscore to confuse Skype:

+1 800 (555) 555 <div style='display: none;'>_</div> 55 55

How to remove “on behalf of” in PHP mail for Outlook 2007 and 2010

December 14th, 2011 by Alex No comments »

Anyone noticed that stupid “admin@domain.com; on behalf of; noreply@domain.com” bug in subject when you received email with Outlook 2007 and 2010?

Well, the fix is pretty simple. While we were setting the From: <email@domain.com> header, now we need to set the Sender header too. So it looks like:

$header='';
$header.="Sender: $from\r\n";
$header.="From: $from\r\n";
$header.="MIME-Version: 1.0\r\n";
$header.="Content-Type: text/html; charset=utf-8\r\n";
$header.="Content-Transfer-Encoding: 8bit\r\n";
 
mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=', $text, $header);

And that’s it. It took me a while googling for the issue to find it, but hopefully it saves your day!

Check if ZIP file is valid with PHP

November 18th, 2011 by Alex No comments »

Recently, i tried to check if ZIP file is valid by checking its MIME type. But server to server, it varies and sometimes even shows up like application/unknown. This is bad, so let’s just rely on native PHP functions:

function zipIsValid($path) {
  $zip = zip_open($path);
  if (is_resource($zip)) {
    // it's ok
    zip_close($zip); // always close handle if you were just checking
    return true;
  } else {
    return false;
  }
}
 
if (zipIsValid('myzip.zip')) {
  // do stuff
  // ...
}

zip_open will return a resource in case of successful opening, otherwise it will return an integer with error code, so checking for if ($zip) won’t be proper.