Bhagwat Singh

PHP Developer Portfolio

PHP

Converting String URLs to Clickable Links with PHP

Recently I came across a problem, I needed to convert a plain text string, that could possibly have a URL in it, to a string with clickable URLs. This is possible with the right PHP code. I created a simple function that takes a parameter of a string and searches for a valid URL (with a regular expression) within it.

function fnConvertToLinks($sInput)
{
$sOutput = preg_replace(
“/(http:\/\/|ftp:\/\/|https:\/\/)([^\s,]*)/i”,
$1$2“,
$sInput
);
return $sOutput;
}

Backup Your MySQL Database Using PHP

This is the most important thing, i think useful for the developers. because at some situation we need customized dump of db or particular tables. here is the way of doing it.

function fnBackup_Tables($host,$user,$pass,$name,$tables)
{
$link = mysql_connect($host,$user,$pass);
mysql_select_db($name,$link);
$return = “”;

// Get all of the tables
if($tables == ‘*’) {
$tables = array();
$result = mysql_query(‘SHOW TABLES’);
while($row = mysql_fetch_row($result)) {
$tables[] = $row[0];
}
} else {
if (is_array($tables)) {
$tables = $tables;
} else {
$tables = explode(‘,’, $tables);
}
}
//var_dump($tables);
// Cycle through each provided table
foreach($tables as $table) {
$result = mysql_query(‘SELECT * FROM ‘.$table);
$num_fields = mysql_num_fields($result);

// First part of the output – remove the table
$return .= ‘DROP TABLE ‘ . $table . “;”;

// Second part of the output – create table
$row2 = mysql_fetch_row(mysql_query(‘SHOW CREATE TABLE ‘.$table));
$return .= “\n\n” . $row2[1] . “;\n\n”;

// Third part of the output – insert values into new table
for ($i = 0; $i < $num_fields; $i++) { while($row = mysql_fetch_row($result)) { $return.= 'INSERT INTO '.$table.' VALUES('; for($j=0; $j<$num_fields; $j++) { $row[$j] = addslashes($row[$j]); $row[$j] = str_replace("\n","\\n",$row[$j]); if (isset($row[$j])) { $return .= '"' . $row[$j] . '"'; } else { $return .= '""'; } if ($j<($num_fields-1)) { $return.= ','; } } $return.= ");\n"; } } $return.="\n"; } mysql_close(); // Close MySQL Connection //this is creating the file for specific date and time. $sFileNameWithPath = 'db_dump'.date('Y-m-d_H:is:').'.sql'; if (file_exists($sFileNameWithPath)) { unlink ($sFileNameWithPath); } file_put_contents($sFileNameWithPath, $return); //return $return; //returning the MYSQL DUMP as a query }

PHP Shorthand If/Else Using Ternary Operators

An essential part of programming is evaluating conditions using if/else and switch/case statements. If / Else statements are easy to code and global to all languages. If / Else statements are great but they can be too long.

here is the shorthand method for this problem. it is called “Ternary operator logic”.


"(condition) ? (true return value) : (false return value)"

Example:
$bResult = ($iVar> 7 ? true : false);

Reading Gmail With PHP IMAP

Hello Friends,

Yesterday i have researched on quite interesting topic that how we can Retrieve Your Gmail Emails Using PHP and IMAP. Here is the simplest solution than using a HUGE MAIL Library.

/* connect to gmail */
$hostname = ‘{imap.gmail.com:993/imap/ssl}INBOX’;
$username = ‘info@bhagwatsingh.com’;
$password = ‘XXXXXXXXXXXX’;

/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die(‘Cannot connect to Gmail: ‘ . imap_last_error());

/* grab emails */
$emails = imap_search($inbox,’ALL’); // ALL OR UNSEEN

/* if emails are returned, cycle through each… */
if($emails) {

/* begin output var */
$output = ”;

/* put the newest emails on top */
rsort($emails);

/* for every email… */
foreach($emails as $email_number) {

/* get information specific to this email */
$overview = imap_fetch_overview($inbox,$email_number,0);
$message = imap_fetchbody($inbox,$email_number,2);

/* output the email header information */
$output.= ‘Subject :’.$overview[0]->subject;
$output.= ‘from :’.$overview[0]->from;
$output.= ‘date on :’.$overview[0]->date;

/* output the email body */
$output.= ‘Message :’.$message;
}

Site Nevigation