Code snippets of PHP and Javascript

in JavaScript, PHP Code / 3 Comments

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;
  }

PHP Web developer with 6 years of experience in Web Development, Integrating web designing, Testing and Deployment , and developed medium level to large scale websites using PHP , and as blogger maintaining technical blog Contact : php.anil@gmail.com

Discussion 3 Comments

  1. Pankaj Patel

    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.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>