» Birthday Bundle - Over $400 worth of Envato files for just $20
  Posts tagged 'base'

PHP: Get Main Base URL

Posted on September 8, 2008, Filled under PHP,  Bookmark it

This is a snippet which extracts the Main Base Address from a complete URL:

<?php
function GetMainBaseFromURL($url)
{
$chars = preg_split('//', $url, -1, PREG_SPLIT_NO_EMPTY);

$slash = 3; // 3rd slash

$i = 0;

foreach($chars as $key => $char)
{
	if($char == '/')
	{
	   $j = $i++;
	}

	if($i == 3)
	{
	   $pos = $key; break;
	}
}

$main_base = substr($url, 0, $pos);

return $main_base.'/';
}

$url = 'http://mysubdomain.mydomain.com/category/hardware/subcategory/mainboards.html';

$main_base = GetMainBaseFromURL($url);

// Output: http://mysubdomain.mydomain.com/

echo $main_base;
?>

PHP: Get Base (Directory) From File (URL, Path)

Posted on September 7, 2008, Filled under PHP,  Bookmark it

If you need to determine the base element from a URL or path of a file then this snippet can help you.

<?php
/*
Source: http://www.bitrepository.com/
*/
function Get_Base_From_File($file)
{
$find = '/';

$after_find = substr(strrchr($file, $find), 1);

$strlen_str = strlen($after_find);

$result = substr($file, 0, -$strlen_str);

return $result;
}

$file = 'http://www.domain.com/path/to/file.zip';

$base = Get_Base_From_File($file);

// Output: http://www.domain.com/path/to/
echo $base;
?>