Posted By

neogeek on 06/30/06


Tagged

textile


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

bahiatronic
vali29


Parse Textile


 / Published in: PHP
 

This is a simple textile function that supports most of the textile syntax. It is in it's early stages and is sure to change.

  1. if (!function_exists('parse_textile')) {
  2.  
  3. function parse_textile($content, $safe_tags = '<a>, <b>, <i>, <u>, <blockquote>, <code>') {
  4.  
  5. $content = ' ' . trim($content) . ' ';
  6.  
  7. $modifiers = Array('\*\*'=>'b', '\__'=>'i', '\*'=>'strong', '\_'=>'em', '\-'=>'del', '\?\?'=>'cite', '%'=>'span', '\+'=>'ins', '\^'=>'sup', '\~'=>'sub', '@'=>'code');
  8.  
  9. $content = strip_tags($content, $safe_tags);
  10.  
  11. $content = ereg_replace('([[:space:]])"[[:<:]](.*)[[:>:]]"([[:space:]])', '\1&#8220;\2&#8221;\3', $content);
  12.  
  13. $content = ereg_replace('([)([[:alnum:]]){1,5}(])', '<sup><a href="#fn\1">\1</a></sup>', $content);
  14.  
  15. $content = ereg_replace('(<p>)(fn([[:alnum:]]){1,}. )([[:alnum:][:punct:][:space:]]+)(<\/p>)', '<p id="fn\3">\4</p>', $content);
  16.  
  17. $content = str_replace(Array('--', '...', ' x ', '(TM)', '(R)', '(C)'), Array('&#8212;', '&#8230;', ' &#215; ', '&#8482;', '&#174;', '&#169;'), $content);
  18.  
  19. while (list($key, $value) = @each($modifiers)) {
  20. $content = ereg_replace('([[:space:]])' . $key . '([[:alnum:]\(\)\*_-~\\+\\^!?\.<\\/>]{1,}([[:alnum:][:space:]\(\)\*_-~\\+\\^!?\.<\\/>"]+)?)' . $key, '\1<' . $value . '>\2</' . $value . '>', $content);
  21. }
  22.  
  23. $content = ereg_replace('([[:alnum:]]+)(\()+([[:alnum:][:space:]]+)(\))', '<acronym title="\3">\1</acronym>', $content);
  24.  
  25. $content = ereg_replace('([[:space:]])((http://)[[:alnum:][:punct:]]+)([[:space:]])?', '\1<a href="\2" rel="nofollow">\2</a>\4', $content);
  26.  
  27. $content = str_replace("\n", "<br />\n", trim($content));
  28.  
  29. return $content;
  30.  
  31. }
  32.  
  33. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: ofadlaoui on November 14, 2010

Thanks for this.

You need to login to post a comment.