hypenate function

Filed Under (codeigniter, php) by admin on 16-01-2011

The hypenated function helps you make a hypenated string.

It replaces the spaces with a hypen (-), good for SEO and clean url string.
Example: “Room nr IT Park and Country Mall”
Becomes: “Room-nr-IT-Park-and-Country Mall”

function hypenate($url){
        $patterns[0]= "/,/";
        $replacements[0]="";
        $patterns[1]="/\s\s+/";
        $replacements[1]="-";
        $url = preg_replace($patterns,$replacements,$url);
        return $url;
}

Usage in PHP

hypenate("The quick brown fox");

output: The-quick-brown-fox

It can also remove out specific string you don’t want to show up on your url e.g. a comma “,” just specify it on the patterns[0] array in regular expression form

 $patterns[0]= "/,/";

Get the clients IP Address in PHP

Filed Under (php) by admin on 03-11-2010

Tagged Under :

This function will get the client’s IP adress

function getClientIP(){

    if (!$_SERVER['HTTP_CLIENT_IP'])
         $ip = $_SERVER['REMOTE_ADDR']
    else
        $ip =$_SERVER['HTTP_CLIENT_IP'];
    return $ip;

}

codeigniter upload “The filetype you are attempting to upload is not allowed”

Filed Under (codeigniter, php) by admin on 13-04-2010

Tagged Under : ,

Error when uploading your file using codeigniter upload
“The filetype you are attempting to upload is not allowed”

Causes:

1. Invalid file type uploaded by the user your uploading (.doc when only .xls is allowed)
2. The file uploaded is not within the specified file types added in the upload config
3. Mime type error in codeigniter caused by the mime type of the extension does not match the mime type specified in /application/config/mimes.php

Start by checking what is the mime type of the file your uploading

$data = array('upload_data' => $this->upload->data());
$mimetype= $data['upload_data']['file_type'];
echo $mimetype;

then check in the mimes.php if that mime type matches the extension you want to upload

Example:
word document

'doc'	=>	'application/msword'

excel file has many mime types, if its missing just add it in the array

'xls'	=>	array('application/excel', 'application/vnd.ms-excel','application/x-msexcel')

PHP file stream

Filed Under (php) by admin on 19-02-2010

Tagged Under :

The code below shows how to open a file from the server which cannot be accessed by the web server

$path = "/home/files/documents.doc";
   header("Cache-Control: pre-check=0, post-check=0, max-age=0");
   header('Content-Type: application/octet-stream');
   header("Content-Disposition: attachment; filename=documents.doc);
   header("Content-Description: File Transfer");
   header("Content-Type:documents.doc);
   header("Last-Modified: " . now());
echo readfile($path);

mt_rand

Filed Under (php) by admin on 25-01-2010

Tagged Under :

The mt_rand() function is a drop-in replacement for rand().
It uses a random number generator with known characteristics using the Mersenne Twister from the php docs website

I somehow noticed php rand() has a pattern, So I dug up the php docs for a better rand function and end up discovering this neat random function

Use mt_rand()

<?php
echo mt_rand() . "\n";
echo mt_rand() . "\n";

echo mt_rand(5, 15);

?>

Output

1604716014
1478613278
6

use Simple_html_dom php library with codeigniter

Filed Under (php) by admin on 22-01-2010

Tagged Under :

I was able to figure out how to run Simple_html_dom php library on codeigniter

Download the simple html dom library here simple html dom

I copied the file Simple_html_dom.php to

/system/libraries

Then in my controller I call it using

require_once(‘Simple_html_dom.php’);

To use it, I initialized it using this line

$html = new Simple_html_dom();

Load a page or link using the line

$html->load_file(‘http://www.google.com’);

and parse or find elements using this line

$html->find(‘table tr td’)->plaintext;

works fine and fits right to what I needed to do.

Convert Excel time to Unix time

Filed Under (php) by admin on 02-12-2009

Tagged Under : , , , ,

When dealing in PHP with imported data from excel spreadsheets , you might encounter that excel dates looks very different.

Excel time looks like this 40179
You cannot compare or compute dates in PHP if it looks like that, it should be on unixtime
which looks like this 1104537600

Here is a quick function to convert excel date to unixtime date

<?
$DayDifference = 25569; //Day difference between 1 January 1900 to 1 January 1970
$Day2Seconds = 86400;// no. of seconds in a day
$ExcelTime = 30128;//integer value stored in the Excel column
$ExcelTime = substr($ExcelTime,0,5);//trim excel time get only whole 5 digit
$UnixTime = ($ExcelTime - $DayDifference)*$Day2Seconds;
echo $UnixTime;
?>

PHP ERROR Allowed memory size of 8388608 bytes exhausted

Filed Under (php) by admin on 27-11-2009

Tagged Under :

Error:
PHP ERROR Allowed memory size of 8388608 bytes exhausted
In php it means your memory use has exceeded the memory limit set by to execute a function.

Solution:
Type this above the function you are trying to excute
ini_set(”memory_limit”,”16M”);
or any desired memory size you wish

PHP 5.3 on Snow Leopard with mysqlnd

Filed Under (mysql, php) by admin on 26-11-2009

Tagged Under : , , ,

On snow leopard the bundled version of PHP installed is 5.3 And it’s new PHP mysql driver is mysqlnd not the old one (ext/mysql)

This might cause the following error:
mysqlnd cannot connect to MySQL 4.1+ using old authentication

Solution:
type in mysql

SET PASSWORD FOR 'some_user'@'some_host' = OLD_PASSWORD('newpwd');

Findings:
Because mysqlnd has a bigger password hash it will not be compatible anymore with the password written beforehand.

Ref:
http://dev.mysql.com/doc/refman/5.1/en/old-client.html

codeigniter read or write a file

Filed Under (codeigniter, php) by admin on 02-11-2009

Tagged Under :

In dealing with files on ci use the file helper
http://codeigniter.com/user_guide/helpers/file_helper.html

It is loaded using this line
$this->load->helper(‘file’);

read a file with read_file()

$data = read_file(‘./path/to/file.php’);

write a file with write_file();
write_file(‘./path/to/file.php’, $data, ‘w+’);

CI recommends “r+” but its better to use “w+”

ads
ads

Jobs at Lexmark Cebu

Software Tools Developer 5 Dec 2008, 1:11 pm

Software Tools Developer

Source: Jobs at Lexmark Jobs at Lexmark |

Company Nurse 5 Dec 2008, 1:11 pm

Company Nurse

Source: Jobs at Lexmark Jobs at Lexmark |

Industrial Designer 5 Dec 2008, 1:11 pm

Industrial Designer

Source: Jobs at Lexmark Jobs at Lexmark |

Data Owner 5 Dec 2008, 1:11 pm

Data Owner

Source: Jobs at Lexmark Jobs at Lexmark |

Firmware Product Systems Engineer (FPSE) 5 Dec 2008, 1:11 pm

Firmware Product Systems Engineer (FPSE)

Source: Jobs at Lexmark Jobs at Lexmark |

Talent Management Specialist 5 Dec 2008, 1:11 pm

Talent Management Specialist

Source: Jobs at Lexmark Jobs at Lexmark |

Business Control Analyst (Auditor) 5 Dec 2008, 1:11 pm

Business Control Analyst (Auditor)

Source: Jobs at Lexmark Jobs at Lexmark |

SAP System Analyst 5 Dec 2008, 1:11 pm

SAP System Analyst

Source: Jobs at Lexmark Jobs at Lexmark |

Firmware Development Engineers 5 Dec 2008, 1:11 pm

Firmware Development Engineers

Source: Jobs at Lexmark Jobs at Lexmark |

HR Officer (Shared Services) 5 Dec 2008, 1:11 pm

HR Officer (Shared Services)

Source: Jobs at Lexmark Jobs at Lexmark |

Technical Writers 5 Dec 2008, 1:11 pm

Technical Writers

Source: Jobs at Lexmark Jobs at Lexmark |

Spanish-speaking Customer Service Representative 5 Dec 2008, 1:11 pm

Spanish-speaking Customer Service Representative

Source: Jobs at Lexmark Jobs at Lexmark |

Technical Illustrator 5 Dec 2008, 1:11 pm

Technical Illustrator

Source: Jobs at Lexmark Jobs at Lexmark |

Recruitment Assistant 5 Dec 2008, 1:11 pm

Recruitment Assistant

Source: Jobs at Lexmark Jobs at Lexmark |

Team Lead 5 Dec 2008, 1:11 pm

Team Lead

Source: Jobs at Lexmark Jobs at Lexmark |

Technical Support Representative 5 Dec 2008, 1:11 pm

Technical Support Representative

Source: Jobs at Lexmark Jobs at Lexmark |

Technical Trainer 5 Dec 2008, 1:11 pm

Technical Trainer

Source: Jobs at Lexmark Jobs at Lexmark |

Software Build and Library Administrator 5 Dec 2008, 1:11 pm

Software Build and Library Administrator

Source: Jobs at Lexmark Jobs at Lexmark |

Business Database Administrator 5 Dec 2008, 1:11 pm

Business Database Administrator

Source: Jobs at Lexmark Jobs at Lexmark |

EDI - Support Analyst 5 Dec 2008, 1:11 pm

EDI - Support Analyst

Source: Jobs at Lexmark Jobs at Lexmark |