Ian M Sutherland

PHP, replace, and regex

In an effort to update this site's code, I realized it would be good opportunity to attempt to re-create the codebase using object oriented programming, rather than functional as it currently is.

In the meantime, I plan to update some of the features of the code that manages the display of these posts. In a previous site meant for posting quotes form an IRC channel (à la bash.org), I wrote a function that took the various tags and formats users would submit, and using regular expressions extrapolate and color-code each line of text (or event chat action) correspondingly. Then trying to identify usernames, and provide a way to automatically search for other posts by that user. In most IRC clients, usernames are surrounded by parentheses. Some users have elevated access to the channel (such as admins, or 'voice' that can chat while other users are muted), which are designated by either a @, +, or %.

The fun part of regular expressions is getting it to finally work. It's honestly funny looking back on previously written code, almost like trying to decipher Egyptian hieroglyphics. As an example:



For regular expressions, each grouping of parentheses represents a set of characters to be used as a variable. I've numbered these to make it easier to follow.
The first slash signifies the start of the entire regular expression. You see the same slash at the end of the regular expression as well.
The first grouping identifies the opening parentheses. Take note of that first \, as I'll come back to it shortly.
The second groups looks for either a @, +, or % symbol, but the question marked at the end means matching these symbols are optional.
Group 3 is any character of the alphabet, both lower and uppercase, any number, and an underscore. The plus at the end here means to make one or more of any of these characters.
Finally is group 4 where we match the end parentheses.

This regex is the first parameter of a preg_replace PHP function. Once the criteria for text that matches all of these is met, PHP will replace the entire string with the text in the next parameter, and replace each instance of $1, $2, $3 and $4 with the corresponding matched text, which happens to be separating the usernames from the rest of the text, and creating a link that will search for other usernames in the database. The \'s that you see here are escape characters so that the quotes and parentheses are not incorrectly considered the end of the parameter.

A similar script is used a few time to match usernames as they appear in various IRC interfaces, such as a username followed by a colon.t9

You can see the current state of this site as I update it ianmsutherland.com/suprbash

Projects coming to github

At the time I purchased this domain, my extent of programming was really javascript and PHP, which some would rather call 'scripting.'

Having a few years of practice with Java now, I thought to look back on my previous work and of the projects I showcased here, only this site remains in existence. Not surprising as none of the sites I designed or created had no revenue streams to stay online. (One domain still remains active, but not having full-time web developer on hand, we switch them to wordpress).

Back in 2011 when I started learning PHP, MySQL, javascript, and CCS for this blog, I don't think I'd heard of github. They only had 2 million repositories - a small fraction of the 20 million they have now. With my Java projects being hosted on github, I plan next to fix some of the missing features I planned for this site, and present this code to github as well. It's not going to compete with Wordpress, but I've learned recently it's better set a goal of compete with myself.

Updating to PHP7

When the backend for this site was created, I was using PHP5. Some of the functions used to connect to my database were still valid, but were becoming obsolete. mysql_* for example was replaced with mysqli_*. Once I attempted to update to PHP7, these functions failed and the site was not functional.

I started by replacing all instances of the various myslq_* functions with mysqli_* using a handy text editor, and in the process realized I could add some extra security with using prepared statements. All functions that have interaction with the database are behind a login, but I thought it would be a good idea to update these database functions though. Thankfully the ease of updating to prepared statements was fairly easy and only took about 15 minutes.

As a side note, it's worth noting the comments I included in the code when I originally wrote this in 2011 were highly instructive in making sure I understood my mindset 10 years ago. All code was procedural as I hadn't learned about object oriented programming yet, so following the logic wasn't difficult. My comments all make as much sense to me now as they did back then, with the exception of one comment for a planned feature:

'//loop for lookout'

Finally I realized I an planned to include a lockout in case of too many failed login attempts. Descriptive comments aren't as useful if they aren't spelled correctly it would seem.

About this site

This site is designed, written and maintain by me using HTML5, CSS3, PHP & MYSQL. The backend is an entirely custom CMS written in PHP.
I can be reached via contact@ianmsutherland.com