Jan 24 2011

PHP Geocoding function, from address to coordinates lat long

Category: Google Maps,Php,Spiders & web botsGiulio Pons @ 5:19 pm

This is a small function included in the Minibots Class that converts an address to a couple of coordinates Latitude, Longitude that can be used to place a marker on a map.
This function uses Google’s geocoding service called with the file_get_contents function (CURL not needed).
The result is decoded with a preg_match call that searches for the center data of the map returned.
You can test the function with this online demo.

// ------------------------------------------
// converts a string with a stret address
// into a couple of lat, long coordinates.
// ------------------------------------------
public function getLatLong($address){
	if (!is_string($address))die("All Addresses must be passed as a string");
	$_url = sprintf('http://maps.google.com/maps?output=js&q=%s',rawurlencode($address));
	$_result = false;
	if($_result = file_get_contents($_url)) {
		if(strpos($_result,'errortips') > 1 || strpos($_result,'Did you mean:') !== false) return false;
		preg_match('!center:\s*{lat:\s*(-?\d+\.\d+),lng:\s*(-?\d+\.\d+)}!U', $_result, $_match);
		$_coords['lat'] = $_match[1];
		$_coords['long'] = $_match[2];
	}
	return $_coords;
}
Share

Tags: , , , , ,


Oct 06 2010

Php Function to resize video from YouTube and Vimeo

Category: PhpGiulio Pons @ 2:31 pm

When you make a web application that let the user insert video embeds coming from YouTube or from Vimeo, or what ever else, there is the problem of the embed size. In fact the object tag (iframe, embed, object) can damage your template and that’s not nice.

You can try to handle the input and the best thing (and also the simplest) you can do is to use regular expression to extract the aspect ratio of the video, remove unwanted tags (such as p tag after the embed that tell the origin of the video) and change the dimensions as you need for your site, preserving template.

I’ve made this function that receive the input $video string that contains the dirty embed code as it is from youtube. The second parameter $new_width (if specified) permits to resize the embed code proportionally to the width you need for your template.
This function removes also unwanted tags added after the important tag (such as happens from Vimeo embeds that have a p tag after the iframe).

function resizeEmbed($video,$new_width='') {
	$video = real_strip_tags($video,array('iframe','embed','param','object'),true);
	preg_match("/width=\"([^\"]*)\"/i",$video,$w); $w = (integer)$w[1];
	preg_match("/height=\"([^\"]*)\"/i",$video,$h); $h = (integer)$h[1];
        if (!$new_width) $new_width = $w;
	$w2 = $new_width;
	$ratio = (float)($w2/$w);
	$h2 = (integer)($h * $ratio);
	$video = str_replace("width=\"$w\"","width=\"$w2\"",$video);
	$video = str_replace("height=\"$h\"","height=\"$h2\"",$video);
	return array("embed"=>$video,"w"=>$w2,"h"=>$h2,"w0"=>$w,"h0"=>$h);
}

As you can see this function uses also this “real strip tags” function that I’ve found on the php.net site:

function real_strip_tags($i_html, $i_allowedtags = array(), $i_trimtext = FALSE) {
	if (!is_array($i_allowedtags)) $i_allowedtags = !empty($i_allowedtags) ? array($i_allowedtags) : array();
	$tags = implode('|', $i_allowedtags);
	if (empty($tags)) $tags = '[a-z]+';
	preg_match_all('@</?\s*(' . $tags . ')(\s+[a-z_]+=(\'[^\']+\'|"[^"]+"))*\s*/?>@i', $i_html, $matches);
	$full_tags = $matches[0];
	$tag_names = $matches[1];
	foreach ($full_tags as $i => $full_tag) {
		if (!in_array($tag_names[$i], $i_allowedtags)) if ($i_trimtext) unset($full_tags[$i]); else $i_html = str_replace($full_tag, '', $i_html);
	}
	return $i_trimtext ? implode('', $full_tags) : $i_html;
}
Share

Tags: , , , ,



« Previous PageNext Page »