Thursday, November 21, 2013

Understanding Regular Expression

Regular expression is the most important part in form validations and it is widely used for search, replace and web crawling systems. If you want to write a selector engine (used to find elements in a DOM), it should be possible with Regular Expressions. In this post we explained few tips that how to understand and write the Regular Expression in simple way.

Will discuss about basic regular expression in three stages.

Stage 1
Symbol             Explanation

^                       Start of string
                      End of string
.                        Any single character
+                       One or more character
\                        Escape Special characters
?                       Zero or more characters

Input exactly match with “abc” 
var A = /^abc$/;

Input start with “abc”
var B = /^abc/;

Input end with “abc”
var C = /abc$/;

Input “abc” and one character allowed Eg. abcx
var D = /^abc.$/;

Input  “abc” and more than one character allowed Eg. abcxy
var E = /^abc.+$/;

Input exactly match with “abc.def”, cause (.) escaped
var F = /^abc\.def$/;

Passes any characters followed or not by “abc” Eg. abcxyz12....
var G = /^abc.+?$/

Stage 2

Char                Group Explanation

[abc]                 Should match any single of character
[^abc]               Should not match any single character
[a-zA-Z0-9]      Characters range lowercase a-z, uppercase A-Z and numbers
[a-z-._]              Match against character range lowercase a-z and ._- special chats 
(.*?)                  Capture everything enclosed with brackets 
(com|info)         Input should be “com” or “info”
{2}                   Exactly two characters
{2,3}                Minimum 2 characters and Maximum 3 characters
{2,}                  More than 2 characters


Put together all in one URL validation.
var URL = /^(http|https|ftp):\/\/(www+\.)?[a-zA-Z0-9]+\.([a-zA-Z]{2,4})\/?/;

URL.test(“http://9lessons.info”);                      // pass
URL.test(“http://www.9lessons.info”);            // pass
URL.test(“https://9lessons.info/”);                   // pass
URL.test(“http://9lessons.info/index.html”);    // pass

Stage 3

Short Form     Equivalent              Explanation 

\d                      [0-9]                         Any numbers
\D                     [^0-9]                       Any non-digits
\w                     [a-zA-Z0-9_]            Characters,numbers and underscore
\W                    [^a-zA-Z0-9_]          Except any characters, numbers and underscore
\s                       -                                White space character
\S                      -                                Non white space character


var number = /^(\+\d{2,4})?\s?(\d{10})$/;  // validating phone number

number.test(1111111111);           //pass
number.test(+111111111111);     //pass
number.test(+11 1111111111);    //pass
number.test(11111111);               //Fail

Redirect The Sub Domain To a Sub Folder with .htaccess

In this post I want to explain " How to redirect the sub domain to a sub folder with .htaccess". I had implemented this forlabs.9lessons.info and demos.9lessons.info. I hope it is useful for you.
Example .htaccess code
You have to replace your sub domain and sub folder name.
RewriteEngine On

RewriteCond %{HTTP_HOST} ^demos\.9lessons\.info$
RewriteCond %{REQUEST_URI} !^/demos/
RewriteRule (.*) /demos/$1

RewriteEngine On

RewriteCond %{HTTP_HOST} ^labs\.9lessons\.info$
RewriteCond %{REQUEST_URI} !^/labs/
RewriteRule (.*) /labs/$1

Htaccess File Inside The Folder

How to use .htaccess file inside the folder. I'm using two .htaccess files in my hosting, one for labs.9lessons and another for touch.9lessons. Just take a look at this post how I had implemented. 

Original URL : http://www.9lessons.info/touch/index.php?id=srinivas
to
Friendly URL : http://touch.9lessons.info/srinivas

First .htaccess file
This code redirects sub domain http://touch.9lessons.info pointing to touch folder.
RewriteEngine On

RewriteCond %{HTTP_HOST} ^touch\.9lessons\.info$
RewriteCond %{REQUEST_URI} !^/touch/
RewriteRule (.*) /touch/$1

Second .htaccess file
This file inside the touch directory. Contains single parameter URL rewriting code. 
RewriteEngine On

RewriteBase /touch/

RewriteEngine On

RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?id=$1


Htaccess File Tutorial and Tips

After posting Understanding of Regular Expression article most of my readers are expecting .htaccess basics, and I did not find any useful article on Google first page results. Specially I love to write.htaccess file, using this you can easily configure and redirect Apache Web Server file system. This post will explain you how to create friendly URLs, sub domain directory re-directions and many more.

Note: .htaccess file will be in hidden format, please change your folder and file settings to view this file. 
How to Create a .htaccess File?
Open any text editor application and file save as with .htaccess name and enablemod_rewrite extension in php.ini file in Apache Web Server configurations. 

Default directory Listing

Disable directory Listing
If you want to disable folder files listing, include following code. 
# Disable Directory Browsing
Options All -Indexes



Error Pages
Here error page is redirecting to error.html
errorDocument 400 http://www.youwebsite.com/error.html
errorDocument 401 http://www.youwebsite.com/error.html
errorDocument 404 http://www.youwebsite.com/error.html
errorDocument 500 http://www.youwebsite.com/error.html

RewriteEngine On it is turn on Rewrite Rules in Apache Server. if you want to turn off, just change the value to off.
RewriteEngine on

Domain Redirection
.htacces code for redirecting yourwebsite.com to www.yourwebsite.com
RewriteCond %{HTTP_HOST} ^yourwebsite.com
RewriteRule (.*) http://www.yourwebsite.com/$1 [R=301,L]

Sub Domain Redirection
Sub domain redirection mapping to folder. Here http://www.yourwebsite.com is connecting to website_folder folder. 
RewriteCond %{HTTP_HOST} ^www\.yourwebsite\.com$
RewriteCond %{REQUEST_URI} !^/website_folder/
RewriteRule (.*) /website_folder/$1

Here http://subdomain.yourwebsite.com is connecting to subdomain_folderfolder. 
RewriteCond %{HTTP_HOST} ^subdomain\.yourwebsite\.com$
RewriteCond %{REQUEST_URI} !^/subdomain_folder/
RewriteRule (.*) /subdomain_folder/$1

Old Domain Redirection
htaccess code for redirecting old domain(abc.com) to new domain(xyz.com). Live demo fglogin.com is now redirecting to oauthlogin.com
RewriteCond %{HTTP_HOST} ^abc.com
RewriteRule (.*) http://www.xyz.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^www\.abc\.com
RewriteRule (.*) http://www.abc.com/$1 [R=301,L]

Friendly URLs
Friendly/Pretty URLs help in search engine rankings.

Profile URL 
Profile parameter allows [a-zA-Z0-9_-] these inputs. More help read Understanding Regular Expression 
http://labs.9lessons.info/profile.php?username=srinivas
to
http://labs.9lessons.info/srinivas
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?username=$1

Messages URL 
http://labs.9lessons.info/messages.php?message_username=srinivas
to
http://labs.9lessons.info/messages/srinivas
RewriteRule ^messages/([a-zA-Z0-9_-]+)$ messages.php?message_username=$1
RewriteRule ^messages/([a-zA-Z0-9_-]+)/$ messages.php?message_username=$1

Friends URL 
http://labs.9lessons.info/friends.php?username=srinivas
to
http://labs.9lessons.info/friends/srinivas
RewriteRule ^friends/([a-zA-Z0-9_-]+)$ friends.php?username=$1
RewriteRule ^friends/([a-zA-Z0-9_-]+)/$ friends.php?username=$1

Friends URL with Two Parameters 
Here the first parameter allows [a-zA-Z0-9_-] and second parameter allows only number [0-9]
http://labs.9lessons.info/friends.php?username=srinivas&page=2
to
http://labs.9lessons.info/friends/srinivas/2
RewriteRule ^friends/([a-zA-Z0-9_-]+)/([0-9]+)$ friends.php?username=$1&page=$2
RewriteRule ^friends/([a-zA-Z0-9_-]+)/([0-9]+)/$ friends.php?username=$1&page=$2

Hiding File Extension
http://www.yourwebsite.com/index.html
to
http://www.yourwebsite.com/index
RewriteRule ^([^/.]+)/?$ $1.html



Friday, February 15, 2013

Jquery simple seconds timer


<script>
var counter = 10;
    setInterval(function() {
        counter--;
     
            document.getElementById("count").innerHTML = 'The page will be refresh after '+counter;
if(counter == 0)
{
             counter=10;
}
    }, 1000);
</script>
<div id="count"></div>

How to refresh DIV using jquery


There are very simple steps to achieve this:
Step 1:
Copy the following code and paste it in the head section of your webpage.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script> 
<script> 
var auto_refresh = setInterval(
function()
{
$('#loaddiv').fadeOut('slow').load('reload.php').fadeIn("slow");
}, 20000);
</script>
Here, above the file “reload.php” will be reloaded in every 20000ms i.e 20 second . You can change the file which you have to reload and you can also change the reload time as per your requirement.
and secondly, the #loaddiv is the name of DIV which is going to be refreshed.
Step 2:
and now you need to put the div in the body section of your page
<div id="loaddiv"> 
</div>
Step 3:
Now finally you need to write the code for the file “reload.php” which will extract the contends from the other page or it also may contain the code to read the data from the database depending upon requirement.
here i am going to read a small content from another site. you can copy , paste the code and save it as the filename “reload.php”. in the same folder that contains the source code.
echo"<img src='http://www.76miles.com/'/>";
Now your page is ready .
Click here to see the demo.
This post is taken from this Articale: http://designgala.com/how-to-refresh-div-using-jquery/ 

How to change the PHP version for subfolders or subdomains

  How to change the PHP version for subfolders or subdomains Setting a specific PHP version for a specific websites, subfolders or subdomain...