Friday, March 19, 2010

Truncate text using php

[sourcecode language="php"]
<?php
function truncate($string, $length='' , $k = ''){
if( $length == '') $length = 50;
if( $k == '') $k = '...';
settype($string, 'string');
settype($length, 'integer');
for($a = 0; $a < $length AND $a < strlen($string); $a++){
$output .= $string[$a];
}
if( strlen($string) > $length)
$output .= $k;
return($output);
}
?>
[/sourcecode]
truncate text using php


$my_string = 'I am kajal Mondol.I live in Kolkata.';
echo truncate($my_string);
echo truncate($my_string,10);
echo truncate($my_string,10,',,,');

This will output:

I am kajal Mondol.I live in Kolkata.
I am kajal...
I am kajal,,,

This function truncates a variable to a character length, default is 50.
'...' is the default text to append if truncation occurs.
Second parameter determines how many characters to truncate to.

Tuesday, March 9, 2010

How to Add a Field to Drupal Contact form

STEP 1 :

First you have to create a module. Create a folder into /sites/all/modules. Give the folder name your module name. Create a module file into the folder. Give the module a name. I have used “contactus.module”. In contactus.module file write the following lines. If you give different module name then the function name should be function yourmodulename_form_alter(&$form, $form_state).


#title    = Title of the input field.

#type     = Type of the input field (e.g. ‘textfield’ or ‘textarea’ etc.  )

#required = If the field is required then value will be true.

Unset function will disable the particular field. Here, in the example I unset copy field. You can re-order all the fields as per need. Just you have to the $ order array. For more information see http://api.drupal.org/api/function/hook_form_alter.

[sourcecode language="php"]

<?php

function contactus_form_alter(&$form, $form_state) {

$form['company'] = array(

'#title' => t('Your company'),

'#type' => 'textfield',

);

$form['phone'] = array(

'#title' => t('Your phone'),

'#type' => 'textfield',

'#required' =>'true',

);

unset($form['copy']);

// reorder the elements in the form to include the elements being inserted

$order = array('name','mail', 'subject', 'company', 'phone','message','submit');

foreach($order as $key => $field) {

$form[$field]['#weight'] = $key;

}

[/sourcecode]

STEP  2: Now you have to create a info file.File name should be your modulename.info . Here I’ve used contactus.info. Into the .info file write the following lines.

name = "contactus"


description = "contact with us"


dependencies[] = contact


core = 6.x


php = 5.1

Thursday, March 4, 2010

Function to create GD thumbnail using Php

function thumbnail($src,$destimagename,$dimension)
{
$src_path=explode("/",$src);
$name=$src_path[count($src_path)-1];
array_pop($src_path);
$oldpath=implode("/",$src_path);
$newpath=$oldpath;
$newpath=$newpath."/".$destimagename;

if(copy($src,$newpath))
{
$name_extension=substr($name,strrpos($name,".")+1);
if (preg_match("/gif/i",$name_extension))
{
$src_img=imagecreatefromgif($newpath);
}
if (preg_match("/jpg|jpeg/i",$name_extension))
{
$src_img=imagecreatefromjpeg($newpath);
}
if (preg_match("/png/i",$name_extension))
{
$src_img=imagecreatefrompng($newpath);
}
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);

$thumb_w = $dimension;
$thumb_h = floor(($old_y/$old_x) * $dimension);

$dst_img=@ImageCreateTrueColor($thumb_w,$thumb_h);
@imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);

if(preg_match("/png/i",$name_extension))
{
@imagepng($dst_img,$newpath);
}
if(preg_match("/jpg|jpeg/i",$name_extension))
{
@imagejpeg($dst_img,$newpath);
}
if(preg_match("/gif/i",$name_extension))
{

@imagegif($dst_img,$newpath);
}
@imagedestroy($dst_img);
@imagedestroy($src_img);

}

}

Wednesday, March 3, 2010

PHP Headers and Popular Mime Types

Atom


header('Content-type: application/atom+xml');

Javascript


header('Content-type: text/javascript');

JPEG Image


header('Content-type: image/jpeg');

PDF


header('Content-type: application/pdf');

XML


header('Content-type: text/xml');

RSS


header('Content-Type: application/rss+xml; charset=ISO-8859-1');

CSS


header('Content-type: text/css');

Create Backup of MySQL Database Using PHP

[sourcecode language="php"]
<?php

$dbName = "db_name";

$dbhost="localhost";

$dbuser="root";

$dbpass="";

$backupFile = $dbName . date("Y-m-d-H-i-s") . '.gz';

$command = "mysqldump --opt -h $dbhost -u $dbuser -p $dbpass $dbName | gzip > $backupFile";

system($command);

header('Content-disposition: attachment; filename='.$backupFile);

header('Content-type: application/x-gzip');

readfile($backupFile);

?>
[/sourcecode]

Configuring The Contact Form Module In Drupal

Drupal has a built in contact form module.

Activate The Module!


Browse to Administer / Site Building / Modules in your site and activate the Contact and Contact Forms module. Save the configuration and you’re ready to go.

Configure The Contact Form


Browse to Administer / Site Building / Contact Form to begin. Click the link at the top of the page to Add Category.




1. Give the category name. I have used Contact Us.

2. Configure recipients. You can put a single email address or multiple email address separated by comma(,).

You can configure the contact us from from the setting options. This page also allows you to set maximum messages per hour – an anti-spam setting that prevents you getting bombarded with contact form spam! You can also allow members to have a personal contact form which relays emails to the address they have in their profile.

Integrating Into Site Navigation


The path of the contact form is yoursite.com/contact. You can link this page from Administer / Site Building / Menus.