JavaScript PHP Code

Code snippets of PHP and Javascript

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

<?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

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

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

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

4 Comments

  • Reply
    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.

  • Reply
    T2Lead
    November 5, 2013 at 6:02 am

    THANKS FOR THIS INFO.

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

  • Reply
    siddhu
    November 18, 2013 at 5:25 pm

    Waiting for this…

  • Reply
    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

This site uses Akismet to reduce spam. Learn how your comment data is processed.