Coding & Camembert

Hi!

I'm a web developer.
I like to document the problems I run into in every day.
This is my knowledge base.

// April 18, 2017

How to close HTML tags in PHP

Ever needed to close a bunch of open HTML tags from a string you are manipulating in PHP? I have!

Specifically in WordPress, I was using the_excerpt() function and needed to allow some tags in the excerpt, unfortunately I ended up having a bunch of un-closed tags that broke my layout. To solve this, I created a function that checks every character in the text for tags.

Once an open tag is found, it is added to an array. When the closing tag appears, I remove the associated tag from the array. The process continues until the loop goes through all of the text. Finally, if there are open tags still store in the array, I reverse the array and return the closing tags.

 

function closing_tags($str) {
    // Check the length of the string
    $strlen = strlen($str);
    // Initialize the variables
    $add_tag = false;
    $rm_tag = false;
    $add_tmp = '';
    $rm_tmp = '';
    $tag_arr[] = '';
    $closing_text = '';

    // Loop through all the characters using the string length
    for ($i = 0; $i <= $strlen; $i++) {
        // Get the character at the current pointer location
        $char = substr($str, $i, 1);

        // Check if it's a open tag <>
        if ($char === '<') {
            $add_tag = true;
            $add_tmp = '';
        }
        // Check if it's a closing tag </>
        else if ($char === '/') {
            $add_tag = false;
            $rm_tag = true;
        }
        // Check if end of the tag or a space in the tag (tag attributes are not needed)
        // If the end of an open tag, add it to the array
        // If the end of a closing tag, find the open tag in the array and remove it
        else if ($char === ' ' || $char === '>') {
            if ($add_tag) {
                $add_tag = false;
                $tag_arr[] = $add_tmp;
            }
            else if ($rm_tag) {
                $rm_tag = false;
                array_splice($tag_arr, array_search($rm_tmp, $tag_arr));
            }
        }
        // If tags haven't ended, keep appending the character to create the tag
        else if ($add_tag) {
            $add_tmp.= $char;
        }
        else if ($rm_tag) {
            $rm_tmp.= $char;
        }
    }

    // Count how many open tags there are
    $arr_size = count($tag_arr);

    // If there are open tags, reverse the array and output them
    // Otherwise, return the original string
    if ($arr_size > 0) {
        $reversed = array_reverse($tag_arr);
        for ($j = 0; $j < $arr_size; $j++) {
            $closing_text.= "</" .
// April 17, 2017

How to Redirect your Domain

Ever need to temporarily, or permanently send visitors to a new domain without asking them to click a new link? There are ways to do this that do not even cause the page to reload. If you are using Apache and have high level SSH access, you can achieve this by editing the configuration files of the specified domains. Alternatively, you can also do a redirect by editing the .htaccess file located in the home directory of your server.

Apache Redirect

This option requires a high level of access to your server. You can try and edit the configuration files using an FTP client, however I would recommend using the command line and SSH into the server to do this. The following directions are assuming you have SSH access.

Locate your “sites-available” directory and edit the .conf file of the website you choose to redirect. Your address may look something similar to this:

sudo nano /etc/apache2/sites-available/somesite.com.conf

The file may look something like this:

<VirtualHost *:80>
    ServerAdmin some@email.com
    ServerName somewebsite.com
    ServerAlias www.somewebsite.com

    DocumentRoot /var/www/public_html
    <Directory />
        Options FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    <Directory /var/www/somewebsite.com/public_html>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

You’ll want to change the lines ServerName and ServerAlias to the following code:

Redirect /oldsite.com http://newsite.com/

If you want to redirect the entire domain, so that oldsite.com/somefolder now links to newsite.com/somefolder, simply use a forward slash instead of the old domain address.

// February 4, 2017

Starting Fresh on DigitalOcean

If you aren’t that well versed in hosting services, let me point you in the direction of my favorite hosting provider: DigitalOcean. If you don’t really know what you are looking at, I recommend you focus on DO’s cost and speed. At $5 USD/month, 20GB (SSD) of space… What more can you ask for? If you need to increase space, it’s very simple, however you cannot downgrade! So start small and upgrade as needed. Each server is called a “droplet” (cute naming association with the brand).

I followed all of the tutorials at Digital Ocean for the 16.04 Ubuntu installation, I will be a bit more specific to how I personally tackled my set-up and the software used.

Tools:

You are not required to use these tools, but I prefer them, and specific commands/functions may only apply to these programs.

Step 1: Sign-up

If you are just interested in testing out the services, the first $10 is on me! Click this link and you will automatically have a credit applied to your account.

// September 13, 2016

Why GoDaddy didn’t work out for me

When I first started out in web development, I didn’t know anything about web hosting. All I knew was that I needed a domain name, and a ‘server’ to host that domain. I started on Yahoo’s Geocities (hello 2008!) and only knew that when you uploaded an .html file, a webpage would appear. When Geocities closed at the end of 2009, I was forced to look into a new hosting provider. Knowing nothing, this is where GoDaddy came in.

I paid $67 USD for the first year, here was the rough breakdown:

  • $48 –  Economy Hosting
  • $5 – Certified Domain
  • $5 – Business Registration
  • $7 – Private Registration Services
  • $2 – Domain Name

Certified domain was/is useless and the business registration I never used. The private registration in theory seems like a nice idea to protect my information.. But can cause legality issues if ever there is a dispute. Sadly, I didn’t understand this, and have been paying for all of those services for the past 5 years. I also ended up increasing my hosting to the Deluxe package (doubling the cost) as I wanted to host multiple domains.