• tutorials
  • php
  • filtering-html-content-contain-to-use-as-page-description

Filtering html content from the database to use as meta tags for SEO

In this tutorial I am going to show you how to reuse the existing content from the database to generate document title and meta description that go into the head section of your template.

It does not really matter what method you use to fetch the data from the database to display web pages. In this tutorial I have a fetched database record assiged to the $record variable - an associated array that contains the needed 'title' and 'content' keys among others that are not going to be used here:

$record['id']
$record['title']
$record['content']
$record['date_published']

In your particular case the name of the array and its keys might be different. For instance in some content managment systems the `title` column in the database named as `headline`, and the `content` as `body`, the variable containing the database record might be named $row, in this senario you'd replace

$record['title'] with $row['headline']
	
and

$record['content'] with $row['body']

Constructing the <title> tag

<title><?php echo trim(strip_tags($record['title'])); ?></title>

• The strip_tags() function is applied to the title to throw off any html tags that might be used within the text, otherwise most web browsers would show verbatim html code in the title bar.

• The trim() function removes any undesirable whitespace that might be before and after the string.

Constructing the <meta name="description" content="..."> tag

<?php // Defining a helper function that merges multiline text // to a single line, additionally converting two or more // whitespace characters into a single one: function merge_multiline_text($string) { // Replace all sorts of new line characters with a space: $single_line_str = str_replace(array("\n", "\r", "%0a", "%0d"), ' ', $string); // Replace two or more subsequent whitespace characters // with a singe space: $value = preg_replace('/ {2,}/', ' ', $single_line_str); // Remove any whitespace characters surrounding // the string and return the result: return trim($value); } // Defining a helper function that shortens text if it's // longer than a given limit and appends '...' to it for // aesthetics: function shorten_long_text($string, $max_num_of_characters) { if (mb_strlen($string) > $max_num_of_characters) { return mb_substr($string, 0, $max_num_of_characters) . '...'; } else { return $string; } } ?> <meta name="description" content="'<?php echo shorten_long_text( merge_multiline_text( htmlspecialchars(strip_tags($record['content']), ENT_COMPAT) ), 300); ?>">

Pay attention that in the shorten_long_text() function I used the mb_strlen() and mb_substr() multi-byte safe functions instead of their strlen() and substr() classical counterparts to reliably count and crop a string that might contain non-English characters as well.

• Just like with the title, first the strip_tags() function is applied to the html content to convert it into plain text.

• In the description we want double-quote characters to be replaces with their corresponding html entities, otherwise your html will break when you have double-quotes, like in the following example:

<meta name="description" content="some text "quoted text" ">

The htmlspecialchars() function with ENT_COMPAT as its second parameter is used to alleviate this problem:

<meta name="description" content="some text &quot;quoted text&quot; ">

• The custom merge_multiline_text() function neatly merges multi-line input returning a single line of text.

• The last function applied to the string is shorten_long_text() that crops longer text (over 300 characters long) appending three dots to its end, you can use a single 'horizontal ellipsis' unicode character instead (&#x2026;) that looks like this:  … . If the length of a string is within the limit, the function returns the given value as is.

And that's all to it. After you've done putting the code into your file, do not forget to look at the raw html code in your browser to see the newly generated tags.

Filtering html content from
the database to use as meta tags for SEO
This page was last updated on January 03, 2025
No comments have been posted so far
Leave a Comment