Header image for Space Out Coding

Code Brackets and Displaying HTML Inside Them

0 Comments

When you're building a CMS, most keep security in mind. Everything that goes into the database is made as harmless as possible. You encode it. This converts the HTML tags into a bunch of special characters. This makes it harmless. You can't inject code into the database.

When it comes out, you decode it, and this lets you use the HTML on the website. Line breaks, divs, making font bold and so on.

But what if you want to display the code, and not have it decoded? That's what the pre and code tags were created for. But they don't decode or encode the HTML. You have to do that yourself.

Using PHP, you can do decode it, and display the HTML that you want.


<?php 
$blog_text = html_entity_decode($blog->text, ENT_QUOTES, 'UTF-8');
$content = preg_replace_callback('/<pre><code>(.*?)<\/code><\/pre>/imsu', function($matches) {
	return str_replace($matches[1], htmlentities($matches[1]),$matches[0]);
}, $blog_text);
echo $content; ?>

And now with this, you're able to use show your HTML between the pre and code tags like so:

<pre> <code> Your HTML Here </code> </pre>

Simple.

Skills

Clientside
  • JavaScript
  • CSS3
  • HTML5
  • jQuery
  • Bootstrap
  • Desktop/Mobile
Serverside
  • PHP
  • MySQLi
  • FuelPHP
  • SEO
  • WordPress
  • OpenCart
  • Shopify
  • MediaWiki

Facebook Twitter 306-972-9054