|
[eluser]Cro_Crx[/eluser]
Hey all
I was in the middle of creating my first CI app and needed to convert safe tags such as [b] into real html tags <b> and I was also looking for a way to automatically highlight code inside [code] tags. I searched online but couldn't find anything, so I've written my own little plugin. I thought it might be useful for someone.
At the moment the plugin converts bold, underline, italic and code safe-tags. It also automatically creates links by default, although this can be turned off by passing an optional value.
The output text is XHTML compliant and is properly formatted in paragraphs
Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter
*
* An open source application development framework for PHP 4.3.2 or newer
*
* @package CodeIgniter
* @author ExpressionEngine Dev Team
* @copyright Copyright (c) 2006, EllisLab, Inc.
* @license http://ellislab.com/codeigniter/user-guide/license.html
* @link http://codeigniter.com
* @since Version 1.0
* @filesource
*/
// ------------------------------------------------------------------------
/*
Instructions:
Load the plugin using:
$this->load->plugin('format_text');
Once the plugin is loaded you can use it with the following code
<?php format_text($text); ?>
OR
<?=format_text($text)?>
Autolink is enabled by default.
There is a second optional parameter that can disable autolink for example:
<?php format_text($text, FALSE); ?>
This will not convert emails/websites to links
*/
function format_text($text, $auto_link = TRUE) {
// ------------------------------------------------------------------------
// First We Replace all of our Pseudo-Tags With Real Tags
$text = str_replace("[b]", "<b>", $text);
$text = str_replace("[/b]", "</b>", $text);
$text = str_replace("[i]", "<i>", $text);
$text = str_replace("[/i]", "</i>", $text);
$text = str_replace("[u]", "<u>", $text);
$text = str_replace("[/u]", "</u>", $text);
$text = str_replace("[code]", "<pre>", $text); // Code is temporarily replaced with <pre>, this gets erased later on anyways. The code used to use <pre> tags, although it uses <code> now and i havn't bothered changing this
$text = str_replace("[/code]", "</pre>", $text);
// ------------------------------------------------------------------------
// Now we run through all the code in our <pre> tags and highlight it, replace the <pre> with <code>
$end = FALSE;
do{
if (!$start = strpos($text, '<pre>'))
$end = TRUE;
if ($end == FALSE)
{
$finish = strpos($text, '</pre>');
$length = $finish - $start + 6;
$code = substr($text, $start, $length);
$remove = array('<pre>', '</pre>');
$code = str_replace($remove, "", $code);
$code = html_entity_decode($code);
$code = highlight_string($code, TRUE);
$text = substr_replace($text, $code, $start, $length);
}
}while($end == FALSE);
// ------------------------------------------------------------------------
// Add some formatting and line breaks
$text = "<p> ".$text."</p>"; // Creates start and end paragraphs
$text = str_replace("\r\n","<br />",$text); // Replaces Blank lines with <br />
$text = str_replace("\n","<br />",$text); // Windows + *nix
// ------------------------------------------------------------------------
// Now we add paragraphs to all text outside of <code> tags
$start = 0;
$end = FALSE;
do {
if (!$finish = strpos($text, '<code>', $start))
$end = TRUE;
$length = $finish - $start ;
$paragraph = substr($text, $start, $length);
$paragraph = str_replace("<br /><br />", "</p><p> ", $paragraph); // Replaces double line break with new paragraph
$text = substr_replace($text, $paragraph, $start, $length);
$start = strpos($text, '</code>', $start +1 );
}while($end == FALSE);
// ------------------------------------------------------------------------
// Finally we add link tags to websites URL's and email addresses (assuming auto link is true)
if ($auto_link == TRUE)
$text = auto_link($text);
return $text; // YAY :) We managed to output our text :)
}
/* End of file format_text_pi.php */
/* Location: ./system/plugins/format_text_pi.php */
BTW: If somone says there's an easier way to do this, or that a plugin has already been written, i'll poke one of my eyes out LOL
|