Category Archives: PHP

Simple RegEx for matching URLs

I wanted a simple regular expression to retrieve URLs from a paragraph of text, but a google search returned so many Regular Expressions that it was tough to separate the good from the bad. So here are two tried and tested Perl Compatible Regular Expressions (PCRE) to help match URLs:

A simple one to match most ‘http’ based links:

(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)

and another more complex one which will match every Link/URL on the face of this planet


([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}|(((news|telnet|nttp|file|http|ftp|https)://)
|(www|ftp)[-A-Za-z0-9]*\\.)[-A-Za-z0-9\\.]+)(:[0-9]*)?/[-A-Za-z0-9_\\$\\.\\+\\!\\*\\(\\),;:@&=\\?/~\\#\\%]*[^]'\\.}>\\),\\\"]

I’ve got a simple PHP function to linkify any URL in a blob of text:

$re = '@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@'; 
$replacement = "<a href='$1'>$1</a>"; 
$formatted_text = preg_replace($re, $replacement, $text); 
echo $formatted_text;

Simple PHP. Any improvements, please post them in the comments.

[PHP] Print out the age of a date in words

I came across a handy piece of code to print out the “age of a date” in words on ThinkPHP. This code should print out the difference between two dates in words (X Days/Months/Weeks/Years). I have modified the original piece of code to print out the number of years as well.

This is a snippet which prints out the difference in words with respective to the current time.

$date_parsed = date_parse($date);
$date_timestamp = mktime(0,0,0, $date_parsed['month'], $date_parsed['day'], $date_parsed['year']);
$now_timestamp = time();
$timestamp_diff = $now_timestamp - $date_timestamp;

if ($timestamp_diff < (60*60*24*7)) {
   echo floor($timestamp_diff/60/60/24)." Days";
}
elseif ($timestamp_diff > (60*60*24*7*4)) {
   echo floor($timestamp_diff/60/60/24/7)." Weeks";
}
else {
   $total_months = $months = floor($timestamp_diff/60/60/24/30);
   if($months >= 12) {
      $months = ($total_months % 12);
      $years  = ($total_months - $months)/12;
      echo $years . " Years ";
   }
   if($months > 0)
      echo $months . " Months";
}

AJAX Resources for Beginners

Here is an updated version of the post – http://rushi.vishavadia.com/blog/2006/04/04/beginning-ajax/. Below is an older version:

Like many people out there, I'm just getting to know the technologies AJAX's hood. For people who don't know about AJAX, Wikipedia gives good info. Ajax in one line is a technology by which you can send and receive data without the page being reloaded, so it looks more 'dynamic'. I guess this could be the dummy way of explaining it.

I wanted to know more about AJAX and learn how to code with AJAX. Just the basic stuff. This is what I found:

Getting Started with AJAX – Author Aaron Gustafson takes you through writing a basic AJAX application (An address book). Lots of code there.

Mastering AJAX – This is a pretty good tutorial which gives you the nitty gritty basics, talks about cross browser compatibility which is really important with XmlHTTPRequest object in IE (stupid MSFT :-p) What I like about this tutorial is that it explains info in a very easy to understand manner and it also says why you need it. (Part 1 (introduction) of the tutorial is here)

Using AJAX with PHP and mySQL – Being a PHP Developer myself, this tutorial is really well written and very complete. Step by step instructions on how to go about working with the XMLHttpRequest object too.

XAJAX – Xajax is an open source PHP Library for building ajax based applications. xajax is very easy to use and damn powerful, allowing you to use php where you wouldnt unneccesarily use JavaScript. They have a very good tutorial here called Learn Xajax in 10 Minutes.

Another good tutorial by John Wiseman on Creating a mySQL connection with PHP and Ajax.(If the code seems too small copy and paste it intoyour favorite editor, or hit Ctrl + '+' on Firefox to increase font size)

Ajax Loading Indicators & Icons – You can make up your own indicator icons, but the ones provided are really nice and fancy.

Lastly, Max Keisler has a very comprehensive list Ajax tutorials, resources – from beginner to advanced.

Leave a comment and tell me what you think about this blog and these articles :)

Edit: The Ajaxian carries a good list of books on Ajax to learn from. 

PHP Editors Ahoy!

I have finally found the PHP Editor of my choice. My months of search for a good one has ended. I am currently using MPSoftware’s PHPDesigner 2005. This is an excellent editor and also may seem to some as a PHP version of MS Visual Studio. It may seem like it but it is very nice. Some of the features what I like is:

  • Auto-Complete PHP functions – Some of the functions get really long!
  • Function/Class List – It lists all the functions and classes of a particular file in a neat window. Very useful for me in some scripts of mine where I have to deal with a lot of functions.
  • Project Setup – I can setup multiple projects and associate files with them and easily open and close projects with relative ease.
  • Code indenting! – This feature a lot of editors dont have. An editor like PHPEdit doesn’t have indenting, which makes it really annoying.

There are also tons of minor handy features which makes it really useful.

A second editor would be Gilad Novak’s PHPStudio. This a lightweight PHP editor which also is quite nice. Auto-Complete, code indentation, a good file explorer and html support are some of its nice features. Gilad Novak reads the PHP-Editors.com forum and answers question in this thread. One of few occasions where a thread guides the development of the product :)

Nevertheless, PHPDesigner 2005 is my preferred editor. I may even check out his beta (’2006′)