Regex Clipboard Class (Mini-Template Engine)


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

https://gist.github.com/4098493
(examples of use below the class code)
This class allows you to load a file of snippets that are really mini-templates copy them from your repository fill the token slots in them the with your data and render them as part of the view. See example below the code for the format of the snippet file.


Copy this code and paste it in your HTML
  1. /**
  2.  * RxClipboard
  3.  * REGEX CLIPBOARD (MIN-TEMPLATE ENGINE)
  4.  * @package ccn
  5.  * @author hskitts
  6.  * @copyright 2012
  7.  * @version $Id$
  8.  * @access public
  9.  */
  10. class RxClipboard extends stdClass{
  11.  
  12. /**
  13.   * RxClipboard::cut()
  14.   *
  15.   * @param mixed $pattern
  16.   * @param mixed $target
  17.   * @param bool $hold_place
  18.   * @return
  19.   */
  20. function cut($pattern,&$target,$hold_place=false){
  21. if(!$hold_place){
  22. $target = preg_replace($pattern,"",$target);
  23. }else{
  24. $target = preg_replace($pattern,$hold_place,$target);
  25. }
  26. return $this;
  27. }
  28.  
  29. /**
  30.   * RxClipboard::copy_between()
  31.   *
  32.   * @param mixed $haystack
  33.   * @param mixed $start
  34.   * @param mixed $end
  35.   * @param string $mod
  36.   * @return
  37.   */
  38. public function copy_between($haystack,$start,$end,$mod='s'){
  39. $pattern ="/$start(.*)$end/$mod";
  40. $this->matches=array();
  41. preg_match_all($pattern,$haystack,$this->matches,PREG_SET_ORDER);
  42. return $this;
  43. }
  44.  
  45. /**
  46.   * RxClipboard::go_until_capture()
  47.   *
  48.   * @param mixed $start
  49.   * @param mixed $until
  50.   * @param mixed $capture
  51.   * @param mixed $context
  52.   * @return
  53.   */
  54. public function go_until_capture($start,$until,$capture,$context){
  55. $reg = "/$start(?:(?!$until).)+($capture)/";
  56. $this->captures=array();
  57. preg_match_all($reg,$context,$this->captures,PREG_SET_ORDER);
  58. return $this;
  59. }
  60.  
  61. /**
  62.   * RxClipboard::paste()
  63.   *
  64.   * @param mixed $target
  65.   * @param mixed $glue_point
  66.   * @param mixed $snippet
  67.   * @return
  68.   */
  69. public function paste(&$target,$glue_point,$snippet) {
  70. $pattern = '/'.$glue_point.'/';
  71. $target = preg_replace($pattern,$snippet,$target);
  72. return $this;
  73. }
  74.  
  75. /**
  76.   * RxClipboard::register_repository()
  77.   *
  78.   * @param mixed $lib_id
  79.   * @param mixed $data
  80.   * @return
  81.   */
  82. public function register_repository($lib_id,$data){
  83. //CACHE A LIBRARY OF SNIPPETS TO COPY AND PASTE
  84. $pattern = '/<!\-\-\scut:(\w+)\s\-\->(.*)?<!\-\-\s\/cut:\1\s\-\->/sU';
  85. $this->matches = array();
  86. preg_match_all($pattern, $data, $this->matches);
  87. $this->{$lib_id} = array_flip($this->matches[1]);
  88. $this->{$lib_id.'_board'} = $this->matches[2];
  89. return $this;
  90. }
  91.  
  92. /**
  93.   * RxClipboard::inPallet()
  94.   *
  95.   * @param mixed $lib_id
  96.   * @param mixed $id
  97.   * @return
  98.   */
  99. private function inPallet($lib_id,$id){
  100. return (boolean) (isset($this->{$lib_id}[$id]));
  101. }
  102.  
  103. /**
  104.   * RxClipboard::get()
  105.   *
  106.   * @param mixed $lib_id
  107.   * @param mixed $id
  108.   * @return
  109.   */
  110. public function get($lib_id,$id){
  111. if ($this->inPallet($lib_id,$id)) {
  112. $snippet = $this->{$lib_id.'_board'}[$this->{$lib_id}[$id]];
  113. return $snippet;
  114. }
  115. }
  116.  
  117. /**
  118.   * RxClipboard::assemble()
  119.   *
  120.   * @param mixed $lib_id
  121.   * @param mixed $list
  122.   * @return
  123.   */
  124. public function assemble($lib_id,$list){
  125. $listItems = explode('|',$list);
  126. $collection = array();
  127. foreach($listItems as $item) {
  128. $collection[] = $this->get($lib_id,$item);
  129. }
  130. return $collection;
  131. }
  132.  
  133. /**
  134.   * RxClipboard::fill()
  135.   *
  136.   * @param mixed $slot
  137.   * @param mixed $data
  138.   * @param mixed $tmpl
  139.   * @return
  140.   */
  141. public function fill($slot, $data,&$tmpl) {
  142. $slot = "#$slot#";
  143. $tmpl = str_replace($slot,$data,$tmpl);
  144. return $this;
  145. }
  146.  
  147. /**
  148.   * RxClipboard::fillAll()
  149.   *
  150.   * @param mixed $array
  151.   * @param mixed $tmpl
  152.   * @return
  153.   */
  154. public function fillAll($array,&$tmpl) {
  155. foreach($array as $key=>$value) {
  156. $this->fill($key,$value,$tmpl);
  157. }
  158. return $this;
  159. }
  160.  
  161. /**
  162.   * RxClipboard::__call()
  163.   *
  164.   * @param mixed $key
  165.   * @param mixed $params
  166.   * @return void
  167.   */
  168. public function __call($key,$params){
  169. if(!isset($this->{$key})) throw new Exception("Call to undefined method ".get_class($this)."::".$key."()");
  170. $subject = $this->{$key};
  171. call_user_func_array($subject,$params);
  172. }
  173.  
  174. /**
  175.   * RxClipboard::xerox()
  176.   *
  177.   * @return
  178.   */
  179. public function xerox() {
  180. return clone($this);
  181. }
  182.  
  183. }
  184.  
  185. //EXAMPLES OF USE
  186. /**
  187. * Basic Usage
  188. */
  189. $RxClip = new RxClipboard(); //instantiate the object
  190.  
  191. //Load A Library Of Snippets
  192. $snippets = file_get_contents("base/pallets/snippets.php");
  193.  
  194. /**
  195. * The Snippet File I Use Is Formatted Like This
  196. <!-- cut:h1 -->
  197. <h1>#HEADLINE#</h1>
  198. <!-- /cut:h1 -->
  199.  
  200. <!-- cut:lead -->
  201. <p class="lead">#CONTENT#</p>
  202. <!-- /cut:lead -->
  203.  
  204. <!-- cut:bodycopy -->
  205. <p>#TEXT1#
  206. #TEXT2#
  207. #TEXT3#
  208. #TEXT4#
  209. </p>
  210. <!-- /cut:bodycopy -->
  211.  
  212. * Each Snippet Is Enclosed is specialy formatted comments
  213. * Each having a unique id by which it is identifed.
  214. */
  215.  
  216. //Get A Snippet From The Repository
  217. $html = $RxClip->get('snippets','h1');
  218.  
  219. //Or Assemble A Collection Of Snippets In An Array
  220. $clipboard = $RxClip->assemble("snippets","h1|lead|bodycopy");
  221.  
  222. //Fill Token Patterns Inside A Snippet
  223. $RxClip->fill("HEADLINE","Regex Clipboard Class",$html);
  224.  
  225. //Or Use An Array Of Values
  226. $plugs = array(
  227. "TEXT1"=>" ONE ",
  228. "TEXT2"=>" TWO ",
  229. "TEXT3"=>" THREE ",
  230. "TEXT4"=>" FOUR "
  231. );
  232. $RxClip->fill("HEADLINE","COPY AND PASTE WEB DESIGN",$clipboard[0]);
  233. $RxClip->fill("CONTENT","Dynamically Create Page Templates And Render Them",$clipboard[1]);
  234. $RxClip->fillAll($plugs,$clipboard[2]);
  235.  
  236. //Render The $clipboard Contents
  237. echo implode(" ",$clipboard);
  238.  
  239. //Copy All Occurences Of Everything Between Two Strings Or Regular Expressions
  240. $RxClip->copy_between($data,"<a","<\/a>");
  241. //returns all anchor tags in $data as a property of #RxClip->matches which is a
  242. //multi-dimensional array
  243.  
  244. //Go From A Starting String/Regex Until Another String/Regex Then Capture A String/Regex Pattern
  245. //If you have several spans like this : <span class="ln">$115.95</span>
  246. //You can get the numericl value in them like this:
  247. $RxClip->go_until_capture("<span",'\"ln"\">\$',"[1234567890]{3}\.[123456789]{2}",$data);
  248. //The results are saved as a propery $RxClip->captures and would look like this :
  249.  
  250. (
  251. [0] => Array
  252. (
  253. [0] => $102.99
  254. [1] => 102.99
  255. )
  256.  
  257. [1] => Array
  258. (
  259. [0] => $110.98
  260. [1] => 110.98
  261. )
  262.  
  263. [2] => Array
  264. (
  265. [0] => $109.99
  266. [1] => 109.99
  267. )
  268. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.