Adding FULLTEXT Search

A few days ago, I wrote an article on how to add a search feature in CodeIgniter projects. The method I used was “Pattern Matching” using SQL’s LIKE statement. Although the LIKE statement works great, it is slow. The search will crawl in situations where there are millions of rows in a table.

An alternative method is using a FULLTEXT index search. It uses SQL’s MATCH AGAINST. To start using a FULLTEXT search, a database table will need to be indexed first. You can accomplish this by altering a table and adding FULLTEXT indexing to certain fields:

Alter Table

ALTER TABLE books ADD FULLTEXT(bookname,author,characters,synopsis);

Once a table has been indexed, you can start using MATCH AGAINST.

Match Against

I tried using CodeIgniter’s active record and MATCH AGAINST, but I could not get them to work. So, I ended up writing out the SQL statement using $this->db->query.

$match = $this->input->post('search');
$searchcount = $this->db->query("SELECT * FROM books WHERE MATCH 
  (bookname,author,characters,synopsis) AGAINST ('.$match.')");

So, if you want to increase search performance, consider using a FULLTEXT search.

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.