Celebrate Pi Day

Pi fans will be celebrating Pi Day all over the world on 14th March. As you already know Pi is a mathamatical constant. Its value is the ratio of any circle’s circumference to its diameter
Today at 1:59 date time will read 3 14 1 59 which is the value of Pi upto 5 decimal places i.e 3.14159. It is observed by the majority of people at 1:59pm though some celebrate at 1:59am too.

Official website for Pi Day – http://www.piday.org/

How to rename files in a directory with php

Recently I came across a situation where I needed to rename more then 100 mp3 files. For some reason I had to remove first 4 characters from each file name. Rather then renaming each file manually, I thought of writing a small php script that renamed all these files for me.
I would like to share that with you, so here goes the code.

$dir = "F:\\Music\\php_test";
$fileType = "mp3";
$charsToTrim = 4;
if($handle = opendir($dir)) 
{
    $count = 1;
    while (false !== ($file = readdir($handle))) // read files in directory one by one
   {
	if(is_file($dir."\\".$file))
	{
		$extension = substr(strrchr($file,"."),1);// get the extension
		if(strcasecmp($fileType,$extension) == 0)// proceed only if it matches with the extension we have provided
		{
			$newName = substr($file,$charsToTrim); // generate new name for file
			$success = rename($dir."\\".$file, $dir."\\".$newName); // rename the file
			if($success)
			{
				echo $file." renamed to ".$newName;
				echo "
"; $count++; } else { echo "Cannot rename ".$file. " to ".$newName."
"; echo "
"; } } } } echo $count." files renamed"; } closedir($handle);

You will have to set these 3 parameters:
$dir – Location of directory in which files are placed.
$fileType – Extension of files that you want to rename(mp3 or txt or doc any other)
$charsToRemove – How many characters do you wish to remove from the filename.

Set these and you are ready to rename. Remember that this script will only trim characters from start of the file name.
This means if you have set $charsToRemove = 5, a file with name “abcd_Plateau” will be renamed to “Plateau”.

Waiting for comments and suggestions.

Create tinyurls in PHP using tinyurl.com API

For all those unfamiliar with tinyurl, it is a service thats lets you convert long urls into short ones. It is very popular and gets about 1.5 billion hits per month. Use of tinyurl can be seen hugely in applications like twitter. As twitter limits users to enter 140 character messages, long urls are a pain to post. Tinyurl reduces them to 25-30 characters. Then they can be posted easily along with other message.

Although it is not mentioned anywhere on TinyURL site, it has an API using which you can write a utility in PHP to create your own tiny urls.

The API can be found at

http://tinyurl.com/api-create.php?url=

It expects a parameter url which is the URL you wish to convert into tiny url. Return value is the generated tiny url in case of success, error otherwise.

Here is the php code:

$url = "http://vijayjoshi.org";
$tiny = file_get_contents("http://tinyurl.com/api-create.php?url=$url");
echo 'Original url : '.$url;
echo '
'; echo '
'; echo 'TinyURL (Click to test) : '.$tiny.'';

In the above case it will output
http://tinyurl.com/cn3c5d

Pretty straightforward. $url contains the url you wish to convert. Then we pass it along with the API url and finally use php function file_get_contents to get the output.

Go ahead and give it a try.

Give thisĀ  article a Thumbs-up on Stumbleupon if you liked it.

FAQ : How to toggle visibility of an html element by javascript

In my previous post, I discussed how we can change the style and/or css class of any html element by JavaScript. We can take advantage of this feature to add functionality where visibility of elements can be toggled. That means clicking a button will change the state of that element from visible to invisible and vice-versa.

The css property that we will manipulate to achieve this behavior is “display”. Below is the html we will use for the example.

<html>
<head>
<title>Toggle elements</title>
<script type="text/javascript" language="javascript">
</script>
</head>
<body>
<div style="border:2px solid;">
<p><input type="button" onClick="toggleMe('toggleHolmes');" value="Click to toggle"/></p>
<p id="toggleHolmes" style="display:block;">
I consider that a mans brain originally is like a little empty attic, and you have to stock it with such furniture as you choose. A fool takes in all the lumber of every sort that he comes across, so that the knowledge which might be useful to him gets crowded out, or at best is jumbled up with a lot of other things, so that he has a difficulty in laying his hands upon it.<br/>
-Sherlock Holmes (A Study in Scarlet - Part 1,chap. 2)
</p>
</div>
<br/>
<div style="border:2px solid;">
<p><input type="button" onClick="toggleMe('toggleNietzsche');" value="Click to toggle"/></p>
<p id="toggleNietzsche" style="display:block;">
God is dead. God remains dead. And we have killed him. How shall we comfort ourselves, the murderers of all murderers? What was holiest and mightiest of all that the world has yet owned has bled to death under our knives: who will wipe this blood off us? What water is there for us to clean ourselves? What festivals of atonement, what sacred games shall we have to invent? Is not the greatness of this deed too great for us? Must we ourselves not become gods simply to appear worthy of it?<br/>
-"The Madman" (Thus Spoke Zarathustra by Friedrich Nietzsche)
</p>

</div>
</body>
</html>

There are 2 paragraphs with ids “toggleHolmes” and “toggleNietzsche” which contain some text. Above each of them is a button. Onclick of each of those buttons we have called a function toggleMe which accepts the id of the element which is to be toggled as its parameter.
Now include the following JavaScript code between the script tags in above html.
Continue reading

Setting style/css class of html elements from javascript

Style of an html element can be set in 2 ways.

  1. Specify a css class name for the element.
  2. Set the style inline for the element.
//Method 1:
<p class="boldText">Some text</p>
//Method 2:
<p style="font-weight:bold;">Some text</p>

Therefore using these 2 attributes we can change the style of any html element in the page. The only thing we need to do is get hold of these attributes in JavaScript and manipulate them.Fortunately accessing a DOM element in JavaScript gives us access to all its attributes. What we get is a DOM object which has the attributes of the element as its properties. So we can perform the same operations we do upon objects.

Changing the css class of an element is quite easy.

Suppose you have a p element on the page and a css defined in your css file

<p> This is some text</p>;
.redText { color : red; }

To change or set the class name for this p

var x = document.getElementById('myPara');
x.className = "redText";

and you are done.

className property changes the class of the specified element. You can specify more then one class, just remember to separate them with a whitespace.

x.className = "redText noBorder";

The other method is setting the inline style for the element. Continue reading