PHP-Snippets

111 (1)

Let’s start with Basic Funda..

Indirect References to variable

$name = "john";
$$name = "register user";
print $john;


next you may want to check that mysql services is running or not using php

echo mysql_stat();


how to make persistent connection ?
-> persistent connection are links that does not close when the execution of scripts end. when persistent connection is requested, PHP checks if there is already persistent connection available or not.. if its available then php use it otherwise it creates link.


$con = mysql_pconnect('localhost','root','');
echo $con;

output: Resource id #31


Read file and replace postgray sql to mysql


$str = file_get_content('sample.txt');
$str = str_replace('postgraysql','mysql',$str);
echo $str;


number of days between two dates.


$date1 = date('y-m-d');
$date2 = '2006-07-01';
$days = (strtotime() - strtotime()) / (60*60*24);
echo $days;


extract string ‘abc.com’ from info@abc.com


preg_match("/.*@c.*)$/","info@abc.com",$data);


Email Validation

<?php
//$email = “abc123@lolhaha”; // Invalid email address
$email = “somebody@somesite.com”; // Valid email address
// Set up regular expression strings to evaluate the value of email variable against
$regex = ‘/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/’;
// Run the preg_match() function on regex against the email address
if (preg_match($regex, $email))
{
echo $email . ” is a valid email. We can accept it.”;
}
else {
echo $email . ” is an invalid email. Please try again.”;
}
?>

Advertisements

2 thoughts on “PHP-Snippets

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s