Hi friends, this post explains about few code snippets used when we develop a web applications.

Code snippets of PHP and Javascript by Anil Kumar Panigrahi

Code snippets of PHP and Javascript by Anil Kumar Panigrahi

Snippet 1 : Generate the random numbers with limits and those numbers shouldn’t be repeat using PHP

PHP Code

1
2
3
4
5
6
7
8
<?php
$num=range(1,10);
   shuffle($num);
   for ($i=0; $i<10; $i++) {    
        echo $num[$i];
        echo "&nbsp";  
   }
?>

Sample Output
2 5 8 9 10 6 3 7 1 4

Snippet 2: Get the file extension using PHP

PHP Code
We can get the two type of snippets from earlier file extension using PHP code

Sample Output
file_extension(“anil.labs.jpg”);
.jpg

Snippet 3: How to clean a string using php code

PHP Code
We can get the code from earlier post how to clean a string using php code

Snippet 4: Addslashes function in Javascript

Javascript Code

1
2
3
4
5
6
7
function addslashes (str) {
    str = str.replace(/'/g, "\'");
    str = str.replace(/\'/g, "\'");
    str = str.replace(/"/g, '\"');
    str = str.replace(/\0/g, '\\0');
    return str;
 }

Sample Output
addslashes(“Anil Kumar Panigrahi’s Blog”);
Anil Kumar Panigrahi\’s Blog

Snippet 5 : Generating Random Password using PHP code

PHP Code
We can get the code from earlier post generating Random Password using PHP code
589sgr

Snippet 6 : How to calculate the list of dates between two dates

PHP Code
We can get the code from earlier post how to calculate the list of dates between two dates

Snippet 7: URL or String encode using Javascript

Javascript Code

1
2
3
4
5
function urlencode (str) {
    str = (str + '').toString();
    return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
    replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
}

Sample Output
urlencode(“Code snippets of PHP and Javascript”);
Code+snippets+of+PHP+and+Javascript

Snippet 8: How to include external js,css files with javascript

Javascript Code
We can get the code from earlier post how to include external js,css files with javascript

Snippet 9: To count the characters in a textarea

Javascript Code
We can get the code from earlier post to count the characters in a textarea using javascript code

Snippet 10: Inserted value is Number or not validation using Javascript

Javascript Code

1
2
3
4
5
6
7
function isNumberKey(evt){
          var charCode=(evt.which) ? evt.which : event.keyCode;
          if (charCode > 31 && (charCode < 48 || charCode > 57)){
             return false;
          }
          return true;
  }

4 Comments

Pankaj Patel · October 29, 2013 at 6:49 am

In snippet 10, there is a inbuilt function inNaN() which returns true if passed variable is not a number and false if the passed number is a nummber.

T2Lead · November 5, 2013 at 6:02 am

THANKS FOR THIS INFO.

WILL USE IT IN MY BLOG http://t2lead.in

siddhu · November 18, 2013 at 5:25 pm

Waiting for this…

Mazid Umar · February 9, 2015 at 5:00 am

Hello,

Nicely described. I will try it to my sites.

Thanks so much for sharing:)

Regards,

Mazid Umar

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *