Thursday, May 28, 2009

Auto expanding textarea

<script src="jquery.js"></script>

<script>
$(document).ready(function(){
$('#wer').scroll(function () {
old_height = document.getElementById("wer").style.height;
document.getElementById("wer").style.height = parseInt(old_height)+10+"px";
});

});
</script>


<textarea style="widows:15px; height:50px;" id="wer">asda sd</textarea>

Tuesday, May 26, 2009

Publishing a blog post using Blogger Data API

// OUTPUT you will get 201 CREATED massege.
// For More HELP please contact me : iamvasim@gmail.com

session_start();
$email = "username@gmail.com";
$pass = "password for blogspot you use";
$blogID= urlencode("your blog id"); // like 6304924319904337556

// Do Not Modify Below Code
if(!isset($_SESSION['sessionToken'])) {

$ch = curl_init("https://www.google.com/accounts/ClientLogin?Email=$email&Passwd=$pass&service=blogger&accountType=GOOGLE");
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1);
curl_setopt($ch, CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,1);
$result = curl_exec($ch);
$resultArray = curl_getinfo($ch);
curl_close($ch);
$arr = explode("=",$result);
$token = $arr[3];
$_SESSION['sessionToken'] = $token;
}



$entry = "<entry xmlns='http://www.w3.org/2005/Atom'><title type='text'>My </title><content type='xhtml'> This is my testing contnet </content></entry>";

$len = strlen($entry);

$headers = array("Content-type: application/atom+xml","Content-Length: {$len}","Authorization: GoogleLogin auth={$_SESSION['sessionToken']}","$entry");

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.blogger.com/feeds/$blogID/posts/default");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, 1);
curl_setopt($ch, CURLOPT_POST, true);
$result = curl_exec($ch);
$ERROR_CODE = curl_getinfo($ch);
curl_close($ch);

echo '<pre>';
print_r($headers);
var_dump($result);
print_r($ERROR_CODE);
exit;

Friday, May 22, 2009

substr

// Examples Of substr function
$string = "123456789"; // our main string

echo substr($string,0,3); // print : 123
// Above example shows starting from 0 (First) position to total 3 characters

echo substr($string,1,3); // print : 234
// Above example shows starting from 1(Second) position to total 3 characters

echo substr($string,-1); // print : 9
// It Will give last character of string

echo substr($string,-4); // print : 6789
// It Will give last 4 characters of string

Thursday, May 21, 2009

How To Remove Array Element

$array = array('hi','how','are','you');
unset($array[1]);

// Now Array will Have

$array = array('hi','are','you');

How To Get All Links From Page Content

$site_url = "http://scriptforphp.blogspot.com"
$string = file_get_contents($site_url);

preg_match_all("|\<a\ href=\"(.*)\"|U",$string, $results); // Get Only URL
preg_match_all("|\<a\ (.*)\<\/a\>|U",$string, $results); // Get Whole Link

$url = $results[1];

foreach($urls as $url)
{
echo htmlentities($url).'<br />';
}

Tuesday, May 12, 2009

Remove HTML Tags From Comment

function strip_tags_attributes($string,$allowtags=NULL,$allowattributes=NULL)
{
if($allowattributes){
if(!is_array($allowattributes))
$allowattributes = explode(",",$allowattributes);
if(is_array($allowattributes))
$allowattributes = implode("|",$allowattributes);
$rep = '/([^>]*) ('.$allowattributes.')(=)(\'.*\'|".*")/i';
$string = preg_replace($rep, '$1 $2_-_-$4', $string);
}
if(preg_match('/([^>]*) (.*)(=\'.*\'|=".*")(.*)/i',$string) > 0){
$string = preg_replace('/([^>]*) (.*)(=\'.*\'|=".*")(.*)/i', '$1$4', $string);
}
$rep = '/([^>]*) ('.$allowattributes.')(_-_-)(\'.*\'|".*")/i';
if($allowattributes)
$string = preg_replace($rep, '$1 $2=$4', $string);
return strip_tags($string,$allowtags);
}

Friday, May 8, 2009

check URL is exist or not

$flag = url_exists("http://www.example.com/somepage.html");

function url_exists($url)
{
$handle = @fopen($url, "r");
if ($handle === false)
{
return false;
}
else
{
return true;
}
fclose($handle);
}

Tuesday, May 5, 2009

Send Mail using mail function in php

$to = "iamvasim@hotmail.com";
$subject = "Php Developer Blog";
$body = "Body of your message here you can use HTML too. e.g. <br> <b> Bold </b>";
$headers = "From: Linknami\r\n";
$headers .= "Reply-To: info@yoursite.com\r\n";
$headers .= "Return-Path: info@yoursite.com\r\n";
$headers .= "X-Mailer: PHP5\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to,$subject,$body,$headers);
?>

Monday, May 4, 2009

Prevent listing files by htaccess

Put below line to prevent listing
Options -Indexes

To prevent zip files
IndexIgnore *.zip

To prevent zip,jpg,gif files
IndexIgnore *.zip *.jpg *.gif

How To Process MySQL Timestamp in PHP

$sql = "select UNIX_TIMESTAMP(date_field) as mydate from mytable where 1=1";
$rs = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($rs))
{
echo $row;
}

how to convert image from BMP to JPG type in php

Step - 1
$upload_path = "your full upload path where image will store";
if($_FILES['file']['type']=='image/bmp')
{
$name=explode('.',$_FILES['file']['name']);
$name=$name[0].".jpg";
$img = ImageCreateFromBmp($upload_path.$_FILES['file']['name']);
imagejpeg($img,$upload_path.$name); // Convert to jpg file
unlink($upload_path.$_FILES['file']['name']); // Delete BMP File

// Your Databse operation here

}

Step - 2
// Now Define function
function ImageCreateFromBmp($filename)
{

$tmp_name = tempnam("/tmp", "GD");

if(bmp2gd($filename, $tmp_name))
{

$img = imagecreatefromgd($tmp_name);

unlink($tmp_name);

return $img;
}
return false;
}


function bmp2gd($src, $dest = false)
{

if(!($src_f = fopen($src, "rb")))
{
return false;
}


if(!($dest_f = fopen($dest, "wb")))
{
return false;
}


$header = unpack("vtype/Vsize/v2reserved/Voffset", fread( $src_f, 14));


$info = unpack("Vsize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyres/Vncolor/Vimportant",
fread($src_f, 40));


extract($info);
extract($header);


if($type != 0x4D42)
{
return false;
}


$palette_size = $offset - 54;
$ncolor = $palette_size / 4;
$gd_header = "";


$gd_header .= ($palette_size == 0) ? "\xFF\xFE" : "\xFF\xFF";
$gd_header .= pack("n2", $width, $height);
$gd_header .= ($palette_size == 0) ? "\x01" : "\x00";
if($palette_size) {
$gd_header .= pack("n", $ncolor);
}

$gd_header .= "\xFF\xFF\xFF\xFF";


fwrite($dest_f, $gd_header);


if($palette_size)
{

$palette = fread($src_f, $palette_size);

$gd_palette = "";
$j = 0;

while($j < $palette_size)
{
$b = $palette{$j++};
$g = $palette{$j++};
$r = $palette{$j++};
$a = $palette{$j++};

$gd_palette .= "$r$g$b$a";
}

$gd_palette .= str_repeat("\x00\x00\x00\x00", 256 - $ncolor);

fwrite($dest_f, $gd_palette);
}


$scan_line_size = (($bits * $width) + 7) >> 3;
$scan_line_align = ($scan_line_size & 0x03) ? 4 - ($scan_line_size & 0x03) : 0;


for($i = 0, $l = $height - 1; $i < $height; $i++, $l--)
{

fseek($src_f, $offset + (($scan_line_size + $scan_line_align) * $l));
$scan_line = fread($src_f, $scan_line_size);
if($bits == 24)
{
$gd_scan_line = "";
$j = 0;
while($j < $scan_line_size)
{
$b = $scan_line{$j++};
$g = $scan_line{$j++};
$r = $scan_line{$j++};
$gd_scan_line .= "\x00$r$g$b";
}
}
elseif($bits == 8)
{
$gd_scan_line = $scan_line;
}
elseif($bits == 4)
{
$gd_scan_line = "";
$j = 0;
while($j < $scan_line_size)
{
$byte = ord($scan_line{$j++});
$p1 = chr($byte >> 4);
$p2 = chr($byte & 0x0F);
$gd_scan_line .= "$p1$p2";
}
$gd_scan_line = substr($gd_scan_line, 0, $width);
}
elseif($bits == 1)
{
$gd_scan_line = "";
$j = 0;
while($j < $scan_line_size)
{
$byte = ord($scan_line{$j++});
$p1 = chr((int) (($byte & 0x80) != 0));
$p2 = chr((int) (($byte & 0x40) != 0));
$p3 = chr((int) (($byte & 0x20) != 0));
$p4 = chr((int) (($byte & 0x10) != 0));
$p5 = chr((int) (($byte & 0x08) != 0));
$p6 = chr((int) (($byte & 0x04) != 0));
$p7 = chr((int) (($byte & 0x02) != 0));
$p8 = chr((int) (($byte & 0x01) != 0));
$gd_scan_line .= "$p1$p2$p3$p4$p5$p6$p7$p8";
}

$gd_scan_line = substr($gd_scan_line, 0, $width);
}

fwrite($dest_f, $gd_scan_line);
}

fclose($src_f);

fclose($dest_f);

return true;
}