Skip to main content

Posts

Showing posts from 2010

jQuery Selectors

In this post I would like to share what are the different ways by which we can select a particular DOM element. There are numerous ways of selecting an element, lets focus on the important ways. Let us consider the following HTML code. <div class="myClass" myAttribute="myAttributeValue"> <span>This is the first span in div </span> <span class="mySpanClass">This is second span in div </span> </div> If you have to get the attribute value of myAttribute of the first div with class="myClass" You can write in two ways - One $("div.myClass").attr('myAttribute'); //output will be myAttributeValue Two $(".myClass").attr('myAttribute'); //output will be myAttributeValue The above second way is not safe if you have elements other than div (such as span,td etc ) with the same class name. Selection by attribute If you want to know the class of the div you can write t...

Running PHP by command line on WAMP

There are a few simple steps one can follow to run php scripts by command line. Usually if you write "php" on the DOS prompt it will not recognize the command. In order to run php on command line in windows you have to do the following - Step 1 : Go to your wamp directory:\wamp\bin\php\php5.x.x (directory depends on your version). Copy this path Step 2 : Right click on My Computer and go to properties > Advanced > Environment Variables > System Variables . In that window select "path" click edit button. Go to the end of "Variable Value" and add a semi-colon ";" and after that paste the path copied. Click "Ok" and close. Step 3 : Open a fresh command prompt . Type php. Now it should run.

Get all dates between two dates

This is a PHP function to get all the dates between two dates. The function will return an array including both the starting and ending date. The returned date format will be Year-month-date(YYYY-MM-DD) just as MySQL standard date format. The function accepts two parameters 1. The start date. 2. The end date. The input date format must acceptable by PHP strtotime function function datesBetween($start_date=null,$end_date=null) { $dates=array(); $dates[0]=date('Y-m-d',strtotime($start_date)); $secs=strtotime($start_date); $i=1; while($secs

Ajax with Jquery

This time I wanted to write something for Jquery. Jquery is a very good Javascript library. It reduces much of your coding time. So here is how you do AJAX with Jquery Step 1 -: Download the latest version of jquery from here. This is Jquery 1.4 minified version. Step 2 -: $.get(url,'',function(data){ alert("Data from ajax is "+data); }); Done !!! Now I will explain it- 1.The "$" is nothing but a shorthand notation for jQuery. 2.Then comes "get" , this can be replaced with post as well, as this is the method by which the http request will be sent to the server. 3.Next is "url". This is the url of the page you are requesting. 4.After url I have the next parameter is left as '' . This parameter is just for adding the parameters (query strings) to the url. It is left to the programmer that he wants to use this parameter or not . You can club your query string in the Url also. 5.Finally we have a ...

PHP Random FIle Name Function

Its always recommended that you rename a file uploaded by users. If it is not done, there is a higher chance that a file of a similar name is already in your directory. To get a random name I have written the function. //Its a good idea to rename the file uploded by the users so as to avoid an overwrite to a file. //We need a random file name (to be generated on the fly) in case when we ask users to upload a file and ... //we do not want to have a clash between file names //this uses time stamp to have a unique name //this function uses Microtime to ensures that file name is unique. //this function can be called even in a for loop, then also it would return unique Name function randomFileName($month=0,$year=0,$random='random') { if(0==$month) $month=date('m'); if(0==$year) $year='RFILE'.date('Y'); if('random'==$random) $random=time();//to create uniqueness $date=date('MjGis');//to create uniqueness $microtime=microtime(true);//to cr...

Extract Image Tags in a page by PHP

This function gets all the image tags in a page f <?php // $content is a string holding the whole html code of the page function getImageTags($content) { $pattern="/<\s*img\s*(.*?)>/i"; preg_match_all($pattern,$content,$matches,PREG_PATTERN_ORDER); return $matches[0]; } ?>

Anchor Text - Extract All of them

This function finds all the href links of a page including their anchor text as well. This function is also capable of finding href links which either have an images or html tags in the text part. I have seen a lot of regular expressions in books and internet ,but they miss out those href links which have images or html tags in them. This gives an array containing the links and their anchor texts . <?php /* $content is an argument which contains the contents of html page in a string. */ function getURLsFromPage($content) { $images=getImageTags($content); foreach($images as $image) { $content=str_replace($image,"",$content); } $anchors=getAnchorText($content); foreach($anchors as $anchor) { $content=str_replace($anchor,strip_tags($anchor),$content); } //get all urls in a page irrespective of their domains... $matches = array(); URL_pattern = "/\s+href\s*=\s*[\"\']?\s*([^\s\"\']+)[\"\'\s]?\s*[^>]*>([^<]*)<\/a>/ims...

Title of the page

This is a PHP function to get the title of the page function getTitle($content) { $url_pattern="/<title>(.*?)<\/title>/ims"; preg_match($url_pattern,$content,$matches); return strip_tags($matches[1]); }

How to write HTML code in blogger ?

It's somewhat tricky to write HTML code in blogger as it does not escape " " characters. So whenever you need to write HTML code or Regular Expression to show someone, you can follow these steps- 1.Go to any of these sites here    or here . These two sites are HTML escaper. 2.Copy the code you have written in any these websites and press the button escape. You will see the escaped code. 3.Now copy this escaped code and paste it again in the new blog post. You are done escaping and press publish to publish the code. And of course, you always have the option of escaping yourself " " character by their escape values. Well this can be an important source of information, if you are willing to do it on your own.

Get all the meta tags of the page

This function gives a well formatted array of meta tags of a page. It can take care of meta tags such as keywords,description,all robots,googlebot etc. <?php function getMetaTags($content) { $return_array=array(); $meta_tags=extractAllMetaTags($content); //get all keywords meta tags $pattern_kw="/\sname\s*=\s*[\"\']*keywords[\"\']*/ims"; $pattern_content="/\scontent\s*=\s*[\"\']?\s*([^\"\']+)[\"\']?(.*?)>/ims"; $return_array['keywords']=array(); foreach($meta_tags[0] as $tag_key=>$tag) { if(preg_match($pattern_kw,$tag)) { if(preg_match($pattern_content,$tag,$matches_content)) { array_push($return_array['keywords'],$matches_content[1]); } } } //get all description meta tags $matches_content=array(); $pattern_description="/\sname\s*=\s*[\"\']*description[\"\']*/ims"; $return_array['description']=array();...

Extract all meta tags

This function extracts all the meta tags in the page. PHP also has a get_meta_tags function but it gives you only those meta tags which have a name attribute. Moreover, PHP's get_meta_tags finds only the last occurence of the tag as in if a keyword meta tag is more than one time in a page then it will not get reported. This function takes care of all these issues. This function will not return array in a very formatted way. If you want to have a well formatted array just read my next post. <?php function extractAllMetaTags($content) { $pattern="/<meta(.*?)>/ims"; preg_match_all($pattern,$content,$matches); return $matches; } ?>

Difference between SJAX and AJAX

AJAX is Asynchronous Javascript and XML SJAX is Synchronous Javascript and XML What does Asynchronous means ? It means that when a request is sent to the server the statements following (javascript code OTHER than the callback function ) are executed WITHOUT waiting for the server response. So even though if your server is taking a long time for responsing for a request , you are not put on a hold. That's the beauty of AJAX. Synchronous means here exactly the opposite of it. That means you force the browser to wait for the server response. This might be a bit a dangerous step because if the server takes a long time,the browser may appear dead. Your users might have to close the tab or the browser. How to send Synchronous request by jQuery? $.ajax method of jquery supports a lot of options , there is an option called async which is true by default . You can set it to false. You can find it here

PHP domain functions

Below is a PHP function which finds the domain of a given url. /* This function takes a url as a parameter and returns the domain of the url. This function also takes care of subdomains Example - if given http://www.exmaple.com/example?sid=1@dd4 this function would return example.com CAUTION- This function does NOT Validates a Url */ function domain($url) { $temp = parse_url($url); if( ! isset($temp['scheme'])) { $url='http://'.$url; $temp=parse_url($url); } if(!empty($temp['host'])) { $urls = $temp['host']; $urls = str_ireplace("www.", "", $temp['host']); return $urls; } else { if(!empty($temp['path'])) { $url = $temp['path']; $url = str_ireplace("www.", "", $temp['path']); return $url; } } }

3 Idiots

The combination of director-producer duo has been fantastic again. After the famous Munnabhai films, Rajkumar Hirani and Vidhu Vinod Chopra jodi is back in 3 Idiots. Don't forget Aamir Khan, who showed again his class of acting and getting into the character. Maddy also looked too good as a normal student struggling to pass the exams. Sharman Joshi, in yet another movie inspired by a novel by Chetan Bhagat. Ok I dont want to fall into their controversy, So I am talking just about the movie. Kareena kampoor plays the role of and doctor cum daughter of the director of the college. The romance in the movie is so cool. The mixup of comedy,emotion and romance is so well mixed up that it keeps the movie altogether on the very next level. Well, talking about getting the movie tickets, it was tough in the first two weeks . In multiplexes, the tickets were expensive.

Nokia E63 (...continued from last post.)

Well, Nokia showed some character. Earlier they told me they would return my repaired phone in 12 working days. But one day(say after 8 working days) I called them just to know the status of my mobile . To my surprise, it already got repaired and it was ready to be given back to me. I was thankful to God and also to Nokia(somewhat) for doing it in time.