CodeIgniter Base URL

I have a CodeIgniter dev site in Google Cloud. The instance obtains a new IP address at every start. You can set the base URL to a domain or IP address in the “/application/config/config.php” file. Since it’s only a dev box, I don’t have a domain assigned. It’s accessible via IP address only which changes every time the instance is started. So here’s the workaround. I added some extra code in the config file to obtain the external IP address.

$realIP = file_get_contents("");
$config['base_url'] = 'http://'.$realIP;

The code is using a service from which returns your external IP address. Works like a charm!

CodeIgniter 2.1.3 on PHP7

If you’re still running older versions of CodeIgniter, here are a couple of tips to make it work with PHP7.

  1. Make sure to install all PHP tools and libraries that comes with PHP7. I was missing curl.
  2. Change your database driver from mysql to mysqli (application/config/config.php).
  3. Fix the /system/core/Common.php error or as some prefer, upgrade to a newer version.

Considering v2.1.3 is very old (currently 3.1.10) , I’m surprised it works with PHP7 with just a few modifications.

Modifying the content of your blog

I wrote a custom blog in CodeIgniter two years ago. I revisited the code to change the content format a bit. I’ve noticed that there were several pieces of code that were commented out. At the time, I was playing around with what I could potentially do with the content. I thought I would share it here, because it will show you how you could drastically change the content of a blog by manipulating the content.

Let’s say the content of your blog is assigned to a variable called $item->content. We can alter the content by passing PHP functions to it. Let’s say we want to limit the content to just the first 330 characters. We can use a PHP function called substr. Substr returns a portion of the string from a specified start and length.

The Excerpt

$item->content = substr($item->content,0,330);
echo $item->content;

In this example, we are limiting content to the first 330 characters.

Remove page breaks and new lines

$item->content = str_replace(array('\r', '\n'), '', $item->content);
echo $item->content;

In this example, str_replace replace page breaks and new lines with ” or nothing.

Remove two spaces

$item->content = str_replace('  ', '', $item->content);
echo $item->content;

Finally, adding Read More […]

$item->content = $item->content . '<a href=#>Read [...]</a>';
echo $item->content;

I appended to content a link with the anchor of ‘Read […].’

URL Spaces in CodeIgniter

I ran into some issues with a piece of code I wrote several years ago in CodeIgniter. I’m using names of people as part of the URL. When someone enters a name with spaces on the database, the spaces are converted to %20 in the resulting URL. The URL doesn’t look pretty and definitely not SEO friendly. It needs to be cleaned up by replacing spaces with either a dash or an underscore.

The other issue is when performing a search, the database doesn’t really know that ‘John Doe’ is essentially the same as ‘John%20Doe.’ So, I ended up using a CodeIgniter function called url_title. The function strips the spaces and replaces it with a dash or a hyphen. In the case of ‘John%20Doe’, it now displays as ‘John-Doe.’ You can also use an underscore. See example below.

Replace %20 with dash or hypen

$name = 'John Doe';
$url = url_title($name);
// produces 'John-Doe'
$name = 'John Doe';
$url = url_title($name, '_');
// produces 'John_Doe'

Put the space before the search

Before performing a search, I use str_replace to remove the dash and put back the space so it matches the database entry. It’s essentially the reverse of what I did to display a friendly URL.

$url = 'John-Doe';
$name = str_replace('-', ' ', $url);
// perform your search function here

If you’re not familiar with CodeIgniter, check how URI routing and url_title are handled.