Skip to main content

Posts

Showing posts from January, 2010

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.