Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Monday, January 24, 2011

Downloading a File using CURL

/*
This is usefull when you are downloading big files, as it
will prevent time out of the script :
*/
set_time_limit(0);
ini_set('display_errors',true);//Just in case we get some errors, let us know....

$fp = fopen (dirname(__FILE__) . '/a.rss', 'w+');//This is the file where we save the information
$ch = curl_init('http://www.webdigity.com/rss.php');//Here is the file we are downloading
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
fclose($fp);

Friday, January 21, 2011

Creating an RSS Feed in PHP

Query from the DB to retrieve the contents. 
$query = "SELECT title, link, description, pubDate FROM
   articles LIMIT 10";
$result = mysql_query($query); 
Then add the XML format as shown below and finally print the xmlString variable. 
 

Generating a PDF using FPDF


 require('fpdf.php');

class PDF extends FPDF
{
//Page header
function Header()
{
    //Logo
    $this->Image('logo.png',20,10,20,20);
    //Arial bold 15
    $this->SetFont('Arial','B',15);
    //Move to the right
    $this->Cell(70);
    //Setting the text in the centre 'C'
    $this->Cell(50,10,'Generating PDF',0,0,'C');
    //Line break
    $this->Ln(20);
}
}

//Instanciation of inherited class
$pdf=new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Arial','',12);
for($i=1;$i<=10;$i++)
    // printing a text center aligned.
    $pdf->Cell(100,10,'Printing line number '.$i,0,1,'C');
$pdf->Output();
?>

Thursday, January 20, 2011

Email address validation in PHP using REGEX

Code to validate:

if (eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$',$_REQUEST['email'])) { 
    echo 'Valid'; 
       } else { 
    echo 'Invalid'; 
       }

Regular Expression Samples

Regx Expression for removing repeated words (case insensitive)

$text = preg_replace("/\s(\w+\s)\1/i", "$1", $text);

Output : ‘Keep your your head’ becomes ‘Keep your head’

Password complexity

Tests if the input consists of 6 or more letters, digits, underscores and hyphens.
The input must contain at least one upper case letter, one lower case letter and one digit.

'\A(?=[-_a-zA-Z0-9]*?[A-Z])(?=[-_a-zA-Z0-9]*?[a-z])(?=[-_a-zA-Z0-9]*?[0-9])[-_a-zA-Z0-9]{6,}\z'

Wednesday, January 12, 2011

Redirecting non www URL to www URL

If you type yahoo.com in browser it will be redirected to www.yahoo.com.
If you want to do same with your website then put the following code to .htaccess file.

CODE:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^optimaxwebsolutions\.com$
RewriteRule (.*) http://www.optimaxwebsolutions.com/$1 [R=301,L]

Where $1 is the first argument kind of thing like for example if you want to redirect

http://drupal.com/user => http://www.drupal.com/user .

Rewriting URL's with .htaccess

Rewriting product.php?id=12 to product-12.html

It is a simple redirection in which .php extension is hidden from the browser’s address bar and dynamic url (containing “?” character) is converted into a static URL.

RewriteEngine on
RewriteRule ^product-([0-9]+)\.html$ product.php?id=$1


For more reference on this refer:
http://roshanbh.com.np/2008/03/url-rewriting-examples-htaccess.html

Setting up a Cron tab in Linux:

Linux Crontab Format
MIN HOUR DOM MON DOW CMD

Example for scheduling a job for 10th June 08:30 AM

30 08 10 06 * /home/ramesh/full-backup

    * 30 – 30th Minute
    * 08 – 08 AM
    * 10 – 10th Day
    * 06 – 6th Month (June)
    * * – Every day of the week

For further more details on setting up Cron tabs and viewing the cron tab entries you can refer:

http://www.thegeekstuff.com/2009/06/15-practical-crontab-examples

Tuesday, January 11, 2011

Writing to a Remote file using FTP functions

$file_name = "data.txt";
$fp = fopen ( $file_name, 'r' );
if ($fp) {
  $data = fread ( $fp, filesize ( $file_name ) );
  fclose ( $fp );
  $file_name = "ftp://user:pass@ftp.somedomain.com/home/user/$file_name";
  $fp = fopen ( $file_name, 'wt' );
  if ($fp) {
    echo 'writing data';
    fwrite ( $fp, $data );
    fclose ( $fp );

  }
}

Customizing the Search form - Drupal

To change the label text and text inside the search box and the text on the submit and changing the Submit button image, you can use the fo...