Blog

The caffeine fuelled writings of a web developer


php-multiple-inputs

Controlling content. Best practices in PHP

Posted on March 22nd 2016  //  Filed under: Web Development

A really quick PHP tip. I was pulling through the first 160 characters of the content in WordPress as our client won’t be using excerpts. With any WordPress content, comes HTML tags as they like to bold certain things, emphasise others, etc.

I was pulling through the content like this:

<p> <?php echo substr($content, 0, 160) . " ..."; ?> </p>

As it happened on a particular news story, the <strong> tag was part of the first 160 characters but the closing tag wasn’t. My browser instantly reacted on load and added one which made my entire footer content bold!

What I should have been doing is this:

<p><?php echo substr(strip_tags($content), 0, 160) . " ..."; ?></p>

This will strip your string of any HTML tags.

You can read more on this PHP method here if you needed to allow a few HTML tags through the filter.

Make sure to remember this! This blog is a more a reminder to myself!

Get in touch

Tweet me or email me as usual!