Mini Textile class


/ Published in: PHP
Save to your folder(s)

This is a version of Textile that only accepts paragraps, bold, italics, links, and images. Enjoy.


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. class TextiLite{
  4.  
  5. /*
  6.   TextiLite:
  7.   A lightweight version of Textile built with PHP by Evan Walsh
  8.   Version 001
  9.  
  10.   Based on the work of:
  11.   http://codeigniter.com/wiki/BBCode_Helper/
  12.   http://codeigniter.com/forums/viewthread/69615/
  13.  
  14.   Supports:
  15.   <br/> by the way of newline
  16.   *Text* => <strong>Text</strong>
  17.   _Text_ => <em>Text</em>
  18.   !http://image.url! => <img src="http://image.url"/>
  19.   "Text":http://text.url => <a href="http://text.url" title="Text">Text</a>
  20.   */
  21.  
  22. function paragraph($text){
  23. $paragraphs = explode("\n\n", $text);
  24. $output = null;
  25. foreach($paragraphs as $paragraph) {
  26. $output .= "\n<p>".$paragraph."</p>\n";
  27. }
  28. return $output;
  29. }
  30.  
  31. function textile($text = null){
  32. $regex = array(
  33. '/(.+)\n(.+)/',
  34. '/\*([^\*]+)\*/',
  35. '/\_([^\*]+)\_/',
  36. '/(!)((?:http|https)(?::\\/{2}[\\w]+)(?:[\\/|\\.]?)(?:[^\\s"]*))(!)/',
  37. '/(")(.*?)(").*?((?:http|https)(?::\\/{2}[\\w]+)(?:[\\/|\\.]?)(?:[^\\s"]*))/',
  38. );
  39. $replace = array(
  40. "$1<br/>$2",
  41. "<strong>$1</strong>",
  42. "<em>$1</em>",
  43. "<img src=\"$2\"/>",
  44. "<a href=\"$4\" title=\"$2\">$2</a>",
  45. );
  46. return preg_replace($regex,$replace,$text);
  47. }
  48.  
  49. function process($text){
  50. $text = $this->paragraph($text);
  51. $text = $this->textile($text);
  52. return $text;
  53. }
  54.  
  55. }
  56.  
  57. ?>

URL: http://nothingconcept.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.