Skip to main content

Using some server-side magic (PHP in this case) and regular expressions, we can utilize GitHub's Gist API to display external code within comments and forum posts as desired!

Read full post...

  Embed format: <script src=""></script>

function embedGists($string) {
  $regex1 = '/';
  $regex2 = '/[gist (d+)]/';
  $replace = '<script src="${1}.js"></script>';

  // Find [gist ######] stuff
  $string = preg_replace($regex1, $replace, $string);
  $string = preg_replace($regex2, $replace, $string);

  return $string;

// Test string
$string = 'lah blah<br />[gist 4575399]<br />And another:';
echo embedGists($string);

/* Provides:

  lah blah<br /><script src=""></script><br />And another: <script src=""></script>