Posted By

evanwalsh on 05/03/08


Tagged

class mini html library textile parser process


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

callum
bahiatronic


Mini Textile class


 / Published in: PHP
 

URL: http://nothingconcept.com

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

  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. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: juliend2 on June 21, 2011

Thanks for this!

Posted By: bluescreenjunky on July 29, 2012

Thanks, I was looking for something like that. I see two minor problems though : -line 35 I'm pretty sure it should be [^_] instead of [^*] since you want any characters except underscores. -line 26 I don't think you need a \n in front of the first

, and it actually produces an extra at the beginning of your text when you use paragraph() then textile().

You need to login to post a comment.