Friday, March 19, 2010

jQuery scroll to element

Here's a simple way to auto scroll to an element of the page really nice.

First you have to add this piece of code (of couse jQuery library must be included in your source code):

jQuery.fn.extend({
scrollTo : function(speed, easing) {
return this.each(function() {
var targetOffset = $(this).offset().top;
$('html,body').animate({scrollTop: targetOffset}, speed, easing);
});
}
});

And then you just scroll to an element id:

$('#element_id').scrollTo(1000);

You can speed up the scrolling by reducing the scroll time. Eg. scrollTo(100)

Thursday, March 4, 2010

PHP log errors to log file

Logging PHP errors to a log file is critical. You don't want to display PHP errors or warnings to users, but to log all errors to a file that you can view. This way you can fix all errors, warnings, notices and the client won't see any PHP error.

You can easily do this by adding two lines at the beginning of your code:

ini_set('log_errors', 1);
ini_set('error_log', '/path-to/error_log.file');

You can use later any editor to view contents of your log file and fix all problems that occured.

Wednesday, February 10, 2010

PHP date/time difference

I have found a great function that returns the difference in days, hours and minutes between 2 dates.

Here is the link, works like a charm:

http://www.gidnetwork.com/b-16.html

For example, this function can be used for events, to know how much time is left until the next event.

Tuesday, August 25, 2009

File_Get_Contents replacement (CURL)

Some servers have file_get_contents function disabled. But don't worry, most of them have curl library installed. Here is a replacement function for file_get_contents, using CURL library. Easy and working:

  1. function file_get_contents_curl($url) {
  2. $ch = curl_init();
  3. curl_setopt($ch, CURLOPT_HEADER, 0);
  4. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  5. curl_setopt($ch, CURLOPT_URL, $url);
  6. $data = curl_exec($ch);
  7. curl_close($ch);
  8. return $data;
  9. }



Using this function is easy, just like file_get_contents function :

$contents = file_get_contents_curl( 'http://...');

Tuesday, August 4, 2009

PHPMailer UTF-8 messages

Sending UTF-8 body messages with PHPMailer is pretty easy. You just have to set charset

Here is an example: $mail->CharSet = 'UTF-8';

So now you can send utf-8 mail messages using PHPMailer. Good luck.

Wednesday, June 24, 2009

How to validate a hex color in PHP

Validating a hex color code in PHP is easy.

Rules:

Hex colors must start with '#' and have 6 characters.
Hex colors can contain the following letters: [A-F]
Hex colors can contain the following digits: [0-9]

So using pregmatch, the hex validation code is:

if(preg_match('/^#[a-f0-9]{6}$/i', $color)) //hex color is valid
{

}

Tuesday, June 23, 2009

PHP Upload and Image Manipulation Class

I've been looking for a simple to use class for uploading and manipulating images. And I have found it.

Here it is: http://www.verot.net/php_class_upload_overview.htm

You can upload files, validate mime types, set language for errors. It's really easy to use. It's free for non-commercial applications.

You can now easily upload and manipulate images (resize, watermark, etc) with this PHP class.