Any Questions?

02476 252 345

A Tidy Way of Limiting Content Snippets With PHP

Dynamic content is brilliant in so many ways, it allows the webmaster to add additional information and images in a click of a button, and ensures that the content on your website is constantly being updated therefore keeping the website fresh. However with every pro there is a con. Without setting in place the right fail safe, it is so very simple to break your website layout by altering text dynamically.

For our example let consider a WordPress site with a dynamic ‘Special Offers’ banner on the homepage. The client makes an alteration however has now added to much content. The special offer now looks unprofessional on the homepage because it overflow out of its container.

Limiting Characters with PHP

The first time I ran into this issue the first thing to occur to me was to use PHP to limit the number of characters, thus solving the problem. For this problem I used the PHP function substr() which allows you to control how many characters of a string you would like to show.

substr(string,start,length)

$string = 'Hello World';

$stringcut = substr($string, 0, 4);

The above code would show the following:

Hello

As you can see this is a great way of controlling the amount of characters you would like to show. Simply alter the the length parameter to your desired preference andĀ Job Done!

If things were only that simple, the issues with the above code are that words can easily be chopped in half which looks messy, especially on banners or feeds.

We need a way of controlling the amount of words being shown.

Limiting Words

The best way to limit the words is to first change the string into an array of words using the PHP function explode(). The stringĀ is divided by the space character.

We then can use the implode() and array_slice() PHP functions to stick the array back together as a string but to control how many words we want to include.

See code below


function limit_words($string, $word_limit) {

// Turns our string into an array, divided by the space character we pass in the first parameter

$words = explode(' ', $string);

// We then chop the words array and stick it back together again as a string.
// The number passed through the $word_limit will be the number of words you would like to show.

return implode(' ', array_slice($words, 0, $word_limit));

}

Pop the above function into your themes functions.php file, and then head over the the PHP code handling the dynamic content. In the example below I will be limiting the WordPress excerpt to show the first 10 words.

<?php echo limit_words(get_the_excerpt(), 10); ?>

This method allows web designers and developers to control any type of dynamic content. On Mash Web Designs eCommerce sites we often limit the ‘Best Sellers’ product descriptions with the exact same code.

Conclusion

So, in this short blog we have covered the best way for web designers to limit their websites dynamic content in order to prevent any layout problems from dynamically added content.

Written by:

Creative Director - info@mash-webdesign.co.uk - 07541987924 - DOB: 02/1993/27

With over five years commercial experience in the web industry, Adam specialises in all things web related. Adam has a passion for CRO, ensuring that the websites he creates don't just look great but also make his clients money.

Mash Web Design