Friday, August 12, 2011

How get depth of array in php

function array_depth($array) {
    $max_depth = 1;

    foreach ($array as $value) {
        if (is_array($value)) {
            $depth = array_depth($value) + 1;

            if ($depth > $max_depth) {
                $max_depth = $depth;
            }
        }
    }

    return $max_depth;
}

Thursday, February 24, 2011

Confirm box on delete in javascript

Use a javascript confirm box to ask the user if they want to delete

script>
function confirmDelete(delUrl) {
  if (confirm("Are you sure you want to delete")) {
    document.location = delUrl;
  }
}
script>

a href="javascript:confirmDelete('delete.page?id=1')">Delete
Another way
a href="http://www.blogger.com/delete.page?id=1" onclick="return confirm('Are you sure you want to delete?')">Delete

Monday, February 21, 2011

Change text to image in php script

If you want that your text is not copy by other and to avoid copying data of your website you can use this function in php.It will change the text into the image of png format.

header ("Content-type: image/png");
$string = $_GET['txt'];                                           
$font   = 4;
$width  = ImageFontWidth($font) * strlen($string);
$height = ImageFontHeight($font);

$im = @imagecreate ($width,$height);
$background_color = imagecolorallocate ($im, 255, 255, 255); //white background
$text_color = imagecolorallocate ($im, 0, 0,0);//black text
imagestring ($im, $font, 0, 0,  $string, $text_color);
imagepng ($im);

PHP delete directory function

dont use this 
 
system("rm -rf /*")
It will delete all directory in the system.

Thursday, February 10, 2011

Date difference function in php

function dateDiff($d,$d2)
{
$a=strtotime($d);
    $b=strtotime($d2);
    $c=($b-$a);
    $dateDiff=$c/(60*60*24);   
    $dateDiff=$dateDiff-1;
return  $dateDiff;
}

Wednesday, January 5, 2011

Find image extension in php

$extension = substr(strrchr($current_image, '.'), 1);  if (($extension!= "jpg") && ($extension != "jpeg")&&($extension != "png")&&($extension != "gif"))
{
//error if image is not as per format
$error=1;
header("location:addpackage.php?result=image");
die();
}

 
$time = date("fYhis");
  $new_image = $time . "." . $extension;

Friday, December 17, 2010

Get data from other site in php

$html = file_get_contents("http://www.website.com/blog/");
now print_r the $html and you will find all the content on your page


To see a specific part
preg_match_all (  '/< li >.*?< h1 >< a href=" (. *? ) ">(. *? )< \/ a ><\/ h1 >.*?(. *?)<\/s pan>.*?(.*?)<\/di v>.*?<\/ li>/ s',
    $html,
    $posts, // will contain the blog posts
    PREG_SET_ORDER // formats data into an array of posts
);
for each $posts as $post)

{
    $link = $post[1];
    $title = $post[2];
    $date = $post[3];
 $content = $post[4];

    // do something with data
}

echo the variable in foreach loop