Getting file extension in php

In this php tutorial you will learn how to get the file extension from a string in php. Sometimes during web development you may need to get the file extension, so let's have a look over how to do this.

There are different ways to do this. Let's have a look over each of them


1) Using explode()

The simplest and easiest way of getting file extension is to use the php built-in function explode.

The explode() function breaks a string into an array.

It takes three parameters in which two of them are required and one is optional.

a) The first required parameter is separator that specifies where to break the string.
b) The second required parameter is string that specifies the string to split.
c) The optional parameter is limit that specifies the maximum number of array elements to return.

So let's have a look over how to get the file extension using explode() method.

<?php
$filename="picture.jpg";
$fileArray=array();
$fileArray =explode('.',$filename);
$file_extension=$fileArray[1];
echo $file_extension;
?>


or you can made a custom php function and call explode() method in it and in the end return the value

<?php
function get_file_extension($file_name)
{
return end(explode('.',$file_name));
}


$filename="picture.jpg";
$file_extension=get_file_extension($filename);
echo $file_extension;
?>

2) Using PATHINFO_EXTENSION

Another way to get the file extension is to use the pathinfo which is an array that contains dirname, basename, extension and file name. Right now we are only interested in the file extension so I am going to call it directly.

<?php
$filename="picture.jpg";
$file_extension = pathinfo($filename,PATHINFO_EXTENSION);
echo $file_extension;
?>


Or you can do like this

<?php
function get_file_extension($file_name)
{
return pathinfo($file_name, PATHINFO_EXTENSION);
}

$filename="picture.jpg";
$file_extension =get_file_extension($filename);
echo $file_extension;
?>

3) Using substring and string char

You can use substring and string char to determine the file name length and position to get the file extension. Let’s have a look over how to do this

<?php
function get_file_extension($file_name)
{
return substr(strrchr($file_name,'.'),1);
}

$filename="picture.jpg";
$file_extension =get_file_extension($filename);
echo $file_extension;
?>

So these are the different ways to get the file extension in php.
Read more...

Redirection in javascript after some time delay

In this programming tutorial we will learn how to redirect the user to another web page or website after some time delay. Its quite easy in javascript as you have to write just single line of code. Lets have a look over the example given below.
Redirection in javascript after some time delay
For this purpose there is a built-in javascript method setTimeout().
setTimeout() method is used to repeatedly call some function after a specified amount of time.

Syntax of setTimeout() Method
setTimeout("Javascript Statement",Milliseconds);

Note: - The setTimeout() is the method of the HTML DOM Window object.

setTimeout() method takes two parameters. The first parameter of setTimeout() is a string that contains a JavaScript statement. This statement could be a statement like "alert('2 seconds!')" or a call to a function, like "helloMsg()".

The second parameter indicates how many milliseconds from now you want to execute the first parameter.

Note: - There are 1000 milliseconds in one second.

So lets have a look over the code
<html>
<head>
<title>Redirection in javascript after some time delay</title>
<script language="javascript" type="text/javascript">
function Delayer()
{
setTimeout('Redirection()', 5000);
}
function Redirection()
{
window.location="http://www.google.com";
}
</script>
</head>
<body>
<form>
<input type="button" value="Redirect after 5 secs" onclick="Delayer();" /></form>
</body>
</html>
The above mentioned code will redirect user to the google.com after 5 seconds of clicking the button. When user will click the button, the function Delayer() will be get call and in this function I am calling Redirection() function after every 5 seconds. So when focus will move to the Redirection() function after every 5 seconds then the user will be immediately redirected to the google.com

So that's it.
I love your feedback.
Read more...

How to disable the browser back button

In this javascript tutorial you will learn how to disable the back button on FireFox's browser or how to prevent the user to go back in FireFox’s browser. Although I am giving force to FireFox's browser yet this code works for all web browsers such as Google Chrome, Opera, Safari, Internet Explorer etc. The reason why I am giving force over FireFox because it is even hard in FireFox to disable the back button or to prevent user to go back.

For example, the below mentioned code will work in other web browsers such as Internet Explorer, Google Chrome but will not perform his responsibility incase of FireFox.


<script language="javascript" type="text/javascript">
window.history.forward(1);
</script>

Also this code will work in other browsers but in case of FireFox it will not work
<script language="javascript" type="text/javascript">
if(history.back)
{
history.go(+1)
}
</script>

So now let's have a look over javascript code that will disable the back button and prevent user to go back in Firefox and in all other browsers

<script language="javascript" type="text/javascript">
function noBack()
{
window.history.forward()
}
noBack();
window.inhibited_load=noBack;
window.onpageshow=function(evt){if(evt.persisted)noBack()}
window.inhibited_unload=function(){void(0)}
</script>

So this is the way to disable the back button on firefox and prevent user to go back.
Read more...

Trim() function in javascript

In this javascript tutorial you will learn how to remove whitespace before /after the string using trim() function in javascript.

Let's have a look over below mentioned javascript custom function that will remove whitespace before/after the string.

function TrimString(str)

{

return str.replace(/^\s+|\s+$/g,"");

}

The calling of this function is very simple, you just have to provide string argument to this function.

I have developed an html web page in which I have a custom function for Trimming which is TrimString() and to call this TrimString() function I have made another function TrimMyString() and I am calling this TrimMyString() function via onclick event of hyperlink.

In this function first of all I declared a variable Myname and assign a string value to it but one thing must be noticed that there are some whitespace before/after the string value and then I calculate the length of that string and store the length in another variable len and then alert the length of the string before the trimming process. After this I used the function TrimString() to trim the string so that whitespace before/after string value can be removed and then again I calculate the length of string after trimming and then alert the length.

Now this time you will find the huge difference between two lengths before and after the trimming process and that difference will tell you how effective and efficient this simple javascript custom function is. Actually this custom function is using regular expression for trimming the whitespace before/after the string, means this function is capable of both ltrim and rtrim.

<html>

<head>

<title>Trim() function in javascript</title>

<script language="javascript" type="text/javascript">

function TrimString(str)

{

return str.replace(/^\s+|\s+$/g,"");

}

function TrimMyString()

{

var Myname,len;

Myname=" Adeel Fakhar ";

len=Myname.length;

alert(len);

Myname=TrimString(Myname);

len=Myname.length;

alert(len);

}

</script>

</head>

<body>

<a href="javascript:TrimMyString();" > Call Trim function</a>

</body>

</html>

So this is the way to trim the string in javascript using trim() function.

Read more...

Passing QueryString from asp.net hyperlink in asp.net using c#

In this asp.net tutorial you will learn how to pass QueryString from asp.net hyperlink in asp.net using c#. Let's have a look over how to do this. In first example we will look how to pass single QueryString from asp.net hyperlink and then in second example we will look how to pass multiple QueryString params from asp.net hyperlink.

First Example:-

<asp:TemplateField HeaderText="Case No">

<ItemTemplate>

<asp:HyperLink ID="hyp_Caseno" Text='<%# Bind("Case_No")%>' runat="server" NavigateUrl='<%# "yourpage.aspx?caseno=" + DataBinder.Eval(Container.DataItem,"Case_No")%>'></asp:HyperLink>

</ItemTemplate>

</asp:TemplateField>

Note:- The Case_No in DataBinder.Eval is the column name of your database’s table. Actually we are getting values of the column from database’s table whenever grid will bind.

Now let’s have a look over how to pass multiple QueryString Params from asp.net hyperlink

Second Example:-

<asp:TemplateField HeaderText="Case No">

<ItemTemplate>

<asp:Hyperlink ID="hyp_Caseno" Text='<%# Bind("Case_No")%>' runat="server" NavigateUrl='<%# "yourpage.aspx?caseno=" + DataBinder.Eval(Container.DataItem,"Case_No") + "&productid=" + DataBinder.Eval(Container.DataItem,"Product_ID")%>' />

</ItemTemplate>

</asp:TemplateField>

So this is the way to pass single and multiple QueryString params from asp.net hyperlink.

Happy Coding

Keep Coding

Read more...