PHP: Making a code block for a blog or forum
Posted September 16, 2010 by Nick Vogt in Programming
Having a code block allows you to display code without it being interpreted as HTML. Code blocks are used extensively on programming-related forums and web sites so users can show other users the code they use. Here is a simple code block:

<html>
  <head>
    <title>H3XED!</title>
    <meta name="description" content="My Blog.">
    <meta name="keywords" content="gaming, stuff">
  </head>
  <body>
    Hello world!
  </body>
<html>

The code block relies on converting various tags into their HTML characters so they do not get ran. You may wonder, why not just use htmlspecialchars to convert the whole contents of the blog or forum post so nothing runs? That works, but not if you have links and images in your post that you do want to run.

To get only the code inside of a code block to be converted, you need to first identify the code block. It is easiest to name your own code tag, like so:

<html>
  <head>
    <title>H3XED!</title>
    <meta name="description" content="My Blog.">
    <meta name="keywords" content="gaming, stuff">
  </head>
  <body>
    Hello world!
    <codeblock><a href="link.html">Some example code</a></codeblock>
  </body>
<html>

The <codeblock> tag is the custom tag you could use to identify your code blocks. You can name it whatever you want, though make sure it is unique of any real HTML tags.

Now that you have a unique tag identifying the part of your post that is code, you can use a PHP function to pick out that area, convert it to html characters, and then insert it back into the post.

function format($body)
{
  // $body is the variable that contains our entire post
  // Use strpos to find the <codeblock> and </codeblock> tags (add 11 to $start so
  // it is at the end of the tag, since <codeblock> is 11 characters long)
  // A space is inserted before $body so that strpos will properly match it
  $start = strpos(' ' . $body, '<codeblock>', 0) + 11;
  $end = strpos($body, '</codeblock>', $start);
  
  // Use substr to put the code block contents into $fragment
  $fragment = substr($body, $start, $end - $start);
  
  // Convert the contents of $fragment using the htmlspecialchars Function
  $fragment = htmlspecialchars($fragment);
  
  // Replace the original content of the code block with $fragment
  // (substr_replace is a very useful function that inserts a string
  // into another string at a specific location)
  $body = substr_replace($body, $fragment, $start, $end - $start);
  
  // Now replace the custom code tags with a div element and return it
  $body = str_replace('<codeblock>', '<div class="code">', $body);
  $body = str_replace('</codeblock>', '</div>', $body);
  return $body;
}

For more information on the substr_replace function and how to use it, see the PHP manual.

That is about it for this guide. To expand the function to find and convert multiple code blocks in the same post, use a loop and make sure that each $start variable starts after the position of the last $end variable, using the offset attribute of the strpos function.

Comment on this post


Features
Free Web MP3 PlayerComputer Build GuidePHP Beginner Tutorials
Post Series
ActionScript 3 TutorialsHard Drive Cost Charts
Popular Tags
actionscriptajaxcall of dutycrysisebayfacebookgooglejavascriptminecraftneweggphprageskyrimtutorialyoutube


H3XED © 2012 Nick Vogt | Web Design
Saturday, May 19, 2012 | Privacy Policy | Disclosure Policy | Contact