Fix `ereg is deprecated` errors in PHP 5.3

To migrate ereg():

ereg('\.([^\.]*$)', $this->file_src_name, $extension);

to

preg_match('/\.([^\.]*$)/', $this->file_src_name, $extension);

To migrate ereg_replace():

$this->file_dst_name_body = ereg_replace('[^A-Za-z0-9_]', '', $this->file_dst_name_body);

to

$this->file_dst_name_body = preg_replace('/[^A-Za-z0-9_]/', '', $this->file_dst_name_body);

To migrate eregi():

eregi('\.([^\.]*$)', $this->file_src_name, $extension);

to

preg_match('/\.([^\.]*$)/i', $this->file_src_name, $extension);

Remove filename from URL path

$main_url = $_SERVER[‘HTTP_REFERER’] ;
$url_explode = explode(“/”, $main_url);
$file_name = end($url_explode);
$file_explode = explode(“.”, $file_name);
$quest_explode = explode(“?”, $file_name);

if(count($file_explode) > 1){
$end_is_file = $file_explode[1];
}
else{
$end_is_file = “”;
}

if(count($quest_explode) > 1){
$end_is_quest = $quest_explode[1];
}
else{
$end_is_quest = “”;
}

if ($end_is_file!= “”){
$result_url = str_replace($file_name, “”, $main_url);
$url = $result_url;
}
elseif ($end_is_quest!= “”){
$result_url = str_replace($file_name, “”, $main_url);
$url = $result_url;
}
else{
if(substr($main_url,-1,1)!= “/”){
$main_url .= “/”;
}
$url = $main_url;
}

Remove index.php in CodeIgniter

Edit the file system/application/config/config.php. Search for

$config[‘index_page’] = “index.php”;

And change to

$config[‘index_page’] = “”;

Create an .htaccess and place it in the root directory.

# Turn on URL rewriting
RewriteEngine On

# Protect application and system files from being viewed
RewriteRule ^(system) – [F,L]

# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT,L]

Calculating Relative Time in PHP

class calculateTime
{
public static function createTimeString($timeStamp, $format=”long”, $dateFormat=”n/j/Y”)
{
// Take the current time and create the difference
$timeDifference = time() – $timeStamp;

// Check the length of time passed since seconds
switch($timeDifference)
{
// Date is less than an hour old
case $timeDifference <= 3600:
$minutes = floor($timeDifference / 60);
$seconds = $timeDifference – ($minutes * 60);

// Choose correct pluralizations
if($minutes == 1)
{
$minuteString = “minute”;
}
else
{
$minuteString = “minutes”;
}

if($seconds == 1)
{
$secondString = “second”;
}
else
{
$secondString = “seconds”;
}

switch($format)
{
case “short”:
return $minutes . “m ” . $seconds . “s ago”;
break;

default:
return $minutes . ” ” . $minuteString . ” ” . $seconds . ” ” . $secondString . ” ago”;
break;
}
break;

// Date is less than a day old
case $timeDifference <= 86400:
$hours = floor($timeDifference / 60 / 60);
$minutes = floor(($timeDifference – ($hours * 60 * 60)) / 60);

// Choose correct pluralizations
if($hours == 1)
{
$hourString = “hour”;
}
else
{
$hourString = “hours”;
}

if($minutes == 1)
{
$minuteString = “minute”;
}
else
{
$minuteString = “minutes”;
}

switch($format)
{
case “short”:
return $hours . “h ” . $minutes . “m ago”;
break;

default:
return $hours . ” ” . $hourString . ” ” . $minutes . ” ” . $minuteString . ” ago”;
break;
}
break;

// Date is less than a week old.
case $timeDifference <= 604800:
$days = floor($timeDifference / 86400);
$hours = floor(($timeDifference – ($days * 86400)) / 60 / 60);

// Choose correct pluralizations
if($days == 1)
{
$dayString = “day”;
}
else
{
$dayString = “days”;
}

if($hours == 1)
{
$hourString = “hour”;
}
else
{
$hourString = “hours”;
}

switch($format)
{
case “short”:
return $days . “d ” . $hours . “h ago”;
break;

default:
return $days . ” ” . $dayString . ” ” . $hours . ” ” . $hourString . ” ago”;
break;
}
break;

// Older than one week. Just return the absolute date.
default:
return date($dateFormat, $timeStamp);
break;
}
}
}

?>