How to Display A URLs Meta Description via A Form Input in PHP

I haven’t posted something code orientated in a while and I thought this is a great snippet to share seeing as it’s something I’ve actually been working on.

Oh, now you’re intrigued?

I’m building an SEO Page Analyser web application. Why? Because I hate the darn ugly interfaces. So I’m not really building it because I need it, I’m building it because I want a better interface.

It will have the same functionality as other SEO site audit tools, just with a cleaner and more user-friendly UI.

Anyway, it’s been a complete learning curve for me building something that’s completely out of my depth and thought it could be of use to some of you (my lovely readers).

Let’s dive in shall we.

You’ll need two pages:

  1. index.php (or whatever you want to call it e.g. form.php) – this page will hold your form, which we are going to use to add a website URL too.
  2. analyse.php – this page will receive the form data and display the meta description held within that pages source code.

Form page (index.php)

So let’s get coding the index.php file:

<form method="GET" action="analyse.php">
    <input type="url" name="target_url" placeholder="Enter a URL..." />
    <input type="submit" value="ANALYSE" />
</form>

We’re using the GET method with our form so we can pass the form data into the URL. This allows users to visit the analyse.php page at a later date using a link with the passed form data within the URL.

Using the POST method would only allow the user to view the page until the page is closed. They would then have to fill the form out again to reach their dedicated analyse.php page with their passed data.

We’re then using the input element and giving it a type of URL. This will only automatically give us some form validation because you won’t be able to access the analyse.php page without entering a valid URL.

You’ll need to fill out the ‘name’ attribute for the input element as this is how we’ll call the passed URL.

And because I wanted to give the option to click a submit rather than a keyboard press, I added in a submit. Remember, you don’t need to have a submit button with only one form element.

Don’t forget to close off your elements properly either. That bugs me.

Display meta description (analyse.php)

Moving on to our second and last file, the analyse.php file which will display our meta description.

<?php 
    $url = $_GET["target_url"];
    $meta = get_meta_tags( $url );
    $description = $meta['description'];
    echo '<p>' . $description . '</p>';
?>

Firstly we’re going to retrieve the URL from the form within a variable called $url. We’re grabbing the URL that was passed through from the form via the predefined variable $_GET and calling the input name attribute (which I mentioned earlier).

We then need to set another variable which uses the get_meta_tags function and adding the URL variable we just set. We add this because we’re defining the location of where we want the meta description to be pulled in from.

Please note, you can actually add a standalone URL here rather than passing data:

<?php 
    $meta = get_meta_tags( 'https://www.google.co.uk/' );
?>

Next we want to define our output so we can display it later. To do this, we’re creating yet another variable and defining what meta type we want to pull in. Obviously we’re using the meta description, but there are other types you can define:

  • Author
  • Keywords
  • Geo Position

And finally, we display the URLs meta description. We do this with echoing the variable we set above. I added HTML paragraph tags as I don’t want it displaying raw text, you should do it too. Don’t be an ugly coder.

Post special: displaying the meta description’s character count

For all of you that wanted to go that extra mile and display the character count of the meta description like SEO Mofo’s SERP Tool does, then read on.

So…

<?php 
    $url = $_GET["target_url"];
    $meta = get_meta_tags( $url );
    $description = $meta['description'];
    echo '<p>' . $description . '</p>';
    // Character count
    $char_count = strlen($description);
    echo '<p>Character count: ' . $char_count . '</p>';
?>

We’ve added two new lines of code now. First we’re going to create a new variable so we can display the character count. In order to display the character count, we need to use the strlen function and assign to pull the character length of our description (which is the string).

And then we’re displaying it using the echo constructor and calling out the $char_count variable to display the count.

That’s all from me folks. Any questions or problems with the snippets above, then please leave a comment.

Displaying the current filename within HTML title tags using PHP

Primarily when I’m on a page, I refer to the page title in the tab display in my browser to remind me of which page I’m on – don’t ask me why! Obviously, I will look at the page content as well.

Recently, I’ve been brainstorming some web app ideas and I was thinking about how I see the page, one thought lead to another… You know how it goes. So it got me thinking, I wouldn’t like to have a column in my SQL table where I have to put in the title for my <title> tag.

What if there was something quicker than going through database connections and all that fun stuff?

Well, I came up with something like this:

  • Create a function 
  • Create an if statement to
    • Display specific title on the index page
    • Display another title on all other pages
  • Call our function within the <title> tags

I did this by getting the name of the file:

<?php echo basename(__FILE__, '.php'); ?>

Essentially, what the above snippet does is, it gets the trailing path of the current file and removes the .php file extension. But, by all means, if you wanted to keep the file extension then you’d have to leave out adding the .php remover:

<?php echo basename(__FILE__); ?>

Now we’ve covered how the logic of the code works, let’s jump into some in depth code.

<?php

function theTitle(){ // Create the function
    if( basename(__FILE__) == "index.php" ) { // Create the if statement
        echo "Dashboard | App Name"; // Display title if page is index.php
    } else { // Or else
        echo (ucfirst(basename(__FILE__, '.php'))) . "&nbsp;| App Name"; // Display other page title
    } // Close if statement
} // Close function

?>

See my code comments, then read the bullets:

  • Creating the function and calling it theTitle
  • Creating the if statement, if on index.php then…
  • Display a UNIQUE page title because it’s the root page
  • Else – if it’s not the index.php / root page, then…
  • Display the current filename, with a capital at the beginning and remove the file extension
  • Close if statement
  • Close function
<?php ucfirst(); ?>

The above code displays a strings first character as a capital letter.

Now the function is in place, I called the function in my <title> tags, in the <head>:

<title><?php theTitle(); ?></title>

What I just did there is call theTitle which is the function I created earlier.

That’s pretty much it, here’s my full code:

<?php

function theTitle(){ // Create the function
    if( basename(__FILE__) == "index.php" ) { // Create the if statement
        echo "Dashboard | App Name"; // Display title if page is index.php
    } else { // Or else
        echo (ucfirst(basename(__FILE__, '.php'))) . "&nbsp;| App Name"; // Display other page title
    } // Close if statement
} // Close function

?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <title><?php theTitle(); ?></title>
    </head>
    <body>
        <p>Body content.</p>
    </body>
</html>

NOTE: I have 2 pages in my demo that I used to create this tutorial, index.php and other.php – they both use the same code, so I’m not going to include 2 snippets below for the two pages.

I hope this can help you guys, it’s a really cool way without having to use databases and all that confusing stuff.