Replace eregi with preg_match

A couple of weeks ago, I wrote a small PHP code to validate if the url being passed is in fact valid. I used a regular expression and PHP’s eregi function to find a pattern match. Looking back, the problem is, the eregi function is deprecated in PHP 5.3.0.

For compatibility going forward, we need to replace eregi with the preg_match function which is better suited for regular expression matches. We will use the same example as last time. As you recall, the function was:

Code With eregi

$urlregex = "^(http|https|ftp)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$";
if (eregi($urlregex, $_POST['url'])) {
//  url is valid. shorten long url
} else {
// reject. url is invalid

Code with preg_match

$urlregex = "^(http|https|ftp)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$";
if(!preg_match($urlregex, $_POST['url']))
// reject. url is invalid //
} else {
url is valid. shorten long url

Here, we’ve sucessfully replaced the eregi function with preg_match.

Form Validation Callbacks Within CodeIgniter

There are many ways to add form validation within CodeIgniter. One way is by extending the form validation library with your own. Another way is by using callbacks. CodeIgniter support callbacks to form validation. I would like to show how callbacks can be added to the existing form validation set of rules.

Validation Rules

Here’s a typical form validation rule where the ‘url’ field is required.

$this->form_validation->set_rules(‘url’, ‘URL’,'required’ );

Validation Rules with Callbacks

This is how you add callbacks. Notice the ‘callback’ prefix.

$this->form_validation->set_rules(‘username’, ‘Username’, ‘callback_url_check’);

The Callback Function

The callback function. The ‘callback’ prefix is omitted.

function url_check() { 
  $regex = “your reg ex code here”;
  if(!preg_match($regex, $str)) {
    set_message(‘valid_url’, ‘Please enter a valid URL.);   return FALSE;
  } else {
    return TRUE;

In the example above, you will need to supply your own regular expression to perform a url match. This is how you implement the callback function. It’s ideal for adding quick functions without extending the current CodeIgniter library.

JQuery Random Lists

I was looking for a JQuery code that would display a random order of unordered list. The HTML markup is displayed below. Each time the page is loaded, a different order of list is displayed.


Here’s the piece of JQuery code that you’ll need add to your webpage to sort and randomize your unordered list.

// get current ul
var $ul = $(this);
// get array of list items in current ul
var $liArr = $ul.children(‘li’);
// sort array of list items in current ul randomly
// Get a random number between 0 and 10
var temp = parseInt( Math.random()*10 );
// Get 1 or 0, whether temp is odd or even
var isOddOrEven = temp%2;
// Get +1 or -1, whether temp greater or smaller than 5
var isPosOrNeg = temp>5 ? 1 : -1;
// Return -1, 0, or +1
return( isOddOrEven*isPosOrNeg );
// append list items to ul

In addition, you can add a class to your unordered list like the example below:

<div class=”sample”>

You will need to change this line to include the class.

$(‘.sample ul’).each(function(){

Delete Wildcards or Directories Recursively

I was cleaning up my Subversion backup which contains hidden .svn directories which I didn’t want. I have about 15 subdirectories, each one having three directories underneath them that contain the .svn directory. To remove all the .svn directories recursively, I needed to perform the command below via the Terminal:

To Delete Files Recursively

If you just want to recursively delete files, for example, that are called “svn.txt” in the svn directory, you will need to use this:

sudo rm 'find -name "svn.txt" | grep svn'

To Delete Directories Recursively

If you want to delete several directories with a same name, using the wildcard. I had to use the “-rf” switch since .svn are directories.

sudo rm -rf 'find -name ".svn" | grep svn'

These commands worked for me. Just to be perfectly sure, backup your files and perform a test first.

Keeping Track of Your Online Accounts

I imagine most people own at least a dozen of accounts online, from email accounts, chat accounts, to online banking, online shopping websites such as Amazon, eBay, Paypal, to Forums, Users groups, Social Media websites, etc. Some users have over 100 accounts. So, how do you keep track of all your usernames, passwords, info, etc? I found an easy way of organizing all my online accounts. I would like to share it here.

Use one email account

I use one email account for all my online activities. Most websites require that you register a valid email address. Using one single email address for all your online activity will make things easier. If you ever have to hunt for a forgotten username or password, you know where to go. To start your recovery, see my next point.

Keep all emails after registration

Most websites that require registration require a valid email address. Websites will usually send out a message or a notice that you have registered an account. Some websites require that you activate the account via an email link. It’s important that you keep these registration messages and not delete them. They sometimes contain usernames and passwords.

Create an “Accounts” folder

The next thing to do is to file all your registration email messages under one folder called “Accounts.” If you have forgetten your username or password in the future, you can always go back to this folder and search for the registration message you received when you first signed up for an account. Sometimes, the registration messages will reveal important information such as username, password or a hint question to get you logged in into an account.

Use one username

To simplify your online experience, you might want to consider using the same username when signing up for accounts. If your name is common, you might want to create something unique. Fortunately, both my firstname and lastname are not very common, so I use the same username for most of my accounts. I say most, because a couple times the account name was already taken. Having the same username may sound like a bad practice for security folks, but it does simplify things. Hence the importance of my next point.

Don’t use the same password for all accounts

Don’t use the same password for all your online accounts. This is bad security practice. If you have over 50 accounts, the practice of one password for each account could go out of hand. So, I recommend that use one password for your email accounts. Use a different password for finance/banking. Use another password for chat accounts, social sites, blogs, etc. You may end up with 5-6 passwords altogether, but it’s not going to be as daunting as having to remember all 50 passwords.

This is how I keep track of all my online accounts. What do you think?

OpenShot: Video Editor For Linux

OpenShot Video Editor is a free, open-source video editor for Linux licensed under the GPL version 3.0. OpenShot can take your videos, photos, and music files and help you create the film you have always dreamed of. Easily add sub-titles, transitions, and effects, and then export your film to DVD, YouTube, Vimeo, Xbox 360, and many other common formats. Download OpenShot.

If you’ve been looking for a video editor for Linux, give it OpenShot a try.

Regex For URL Validation

I just wanted to share a regular expression that works for me. I was just trying to shorten a URL from Nike’s website using my own URL shortener. There was a slight problem. My script was rejecting the long URL because it was not in the right format, but there was nothing wrong with Nike’s URL. My regex was faulty. Time for a change. So, I searched online for a better regex to work with my URL shortener script. I found this code from the library that works for me.



It’s a handful. All in one line by the way.

URL Validation

Assuming we are using a HTML form to submit the long URL, here’s how to validate the URL with the help of PHP’s eregi function. The eregi function is a case insensitive regular expression matching function. If the posted URL matches the regular expression, then the statement is true. We can then shorten the URL. If the URL is invalid, we reject the URL and send out a message saying the URL is not in the correct format.

$urlregex = “^(http|https|ftp)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&amp;%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|me|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\’\\\+&amp;%\$#\=~_\-]+))*$”;

if (eregi($urlregex, $_POST[‘url’])) {
//  url is valid. shorten long url
} else {
// reject. url is invalid

Matches |’s | | |’s |

One last thing, You can add more TLDs and country code to the regular expression. I added the .me domain since one of my domains is a .me.