正则表达式剪贴板类(迷你模板引擎)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了正则表达式剪贴板类(迷你模板引擎)相关的知识,希望对你有一定的参考价值。
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.
/** * RxClipboard * REGEX CLIPBOARD (MIN-TEMPLATE ENGINE) * @package ccn * @author hskitts * @copyright 2012 * @version $Id$ * @access public */ class RxClipboard extends stdClass{ /** * RxClipboard::cut() * * @param mixed $pattern * @param mixed $target * @param bool $hold_place * @return */ function cut($pattern,&$target,$hold_place=false){ if(!$hold_place){ }else{ } return $this; } /** * RxClipboard::copy_between() * * @param mixed $haystack * @param mixed $start * @param mixed $end * @param string $mod * @return */ public function copy_between($haystack,$start,$end,$mod='s'){ $pattern ="/$start(.*)$end/$mod"; return $this; } /** * RxClipboard::go_until_capture() * * @param mixed $start * @param mixed $until * @param mixed $capture * @param mixed $context * @return */ public function go_until_capture($start,$until,$capture,$context){ $reg = "/$start(?:(?!$until).)+($capture)/"; return $this; } /** * RxClipboard::paste() * * @param mixed $target * @param mixed $glue_point * @param mixed $snippet * @return */ public function paste(&$target,$glue_point,$snippet) { $pattern = '/'.$glue_point.'/'; return $this; } /** * RxClipboard::register_repository() * * @param mixed $lib_id * @param mixed $data * @return */ public function register_repository($lib_id,$data){ //CACHE A LIBRARY OF SNIPPETS TO COPY AND PASTE $pattern = '/<!--scut:(w+)s-->(.*)?<!--s/cut:1s-->/sU'; $this->{$lib_id.'_board'} = $this->matches[2]; return $this; } /** * RxClipboard::inPallet() * * @param mixed $lib_id * @param mixed $id * @return */ private function inPallet($lib_id,$id){ } /** * RxClipboard::get() * * @param mixed $lib_id * @param mixed $id * @return */ public function get($lib_id,$id){ if ($this->inPallet($lib_id,$id)) { $snippet = $this->{$lib_id.'_board'}[$this->{$lib_id}[$id]]; return $snippet; } } /** * RxClipboard::assemble() * * @param mixed $lib_id * @param mixed $list * @return */ public function assemble($lib_id,$list){ foreach($listItems as $item) { $collection[] = $this->get($lib_id,$item); } return $collection; } /** * RxClipboard::fill() * * @param mixed $slot * @param mixed $data * @param mixed $tmpl * @return */ public function fill($slot, $data,&$tmpl) { $slot = "#$slot#"; return $this; } /** * RxClipboard::fillAll() * * @param mixed $array * @param mixed $tmpl * @return */ public function fillAll($array,&$tmpl) { foreach($array as $key=>$value) { $this->fill($key,$value,$tmpl); } return $this; } /** * RxClipboard::__call() * * @param mixed $key * @param mixed $params * @return void */ public function __call($key,$params){ $subject = $this->{$key}; } /** * RxClipboard::xerox() * * @return */ public function xerox() { return clone($this); } } //EXAMPLES OF USE /** * Basic Usage */ $RxClip = new RxClipboard(); //instantiate the object //Load A Library Of Snippets /** * The Snippet File I Use Is Formatted Like This <!-- cut:h1 --> <h1>#HEADLINE#</h1> <!-- /cut:h1 --> <!-- cut:lead --> <p class="lead">#CONTENT#</p> <!-- /cut:lead --> <!-- cut:bodycopy --> <p>#TEXT1# #TEXT2# #TEXT3# #TEXT4# </p> <!-- /cut:bodycopy --> * Each Snippet Is Enclosed is specialy formatted comments * Each having a unique id by which it is identifed. */ //Get A Snippet From The Repository //Or Assemble A Collection Of Snippets In An Array $clipboard = $RxClip->assemble("snippets","h1|lead|bodycopy"); //Fill Token Patterns Inside A Snippet $RxClip->fill("HEADLINE","Regex Clipboard Class",$html); //Or Use An Array Of Values "TEXT1"=>" ONE ", "TEXT2"=>" TWO ", "TEXT3"=>" THREE ", "TEXT4"=>" FOUR " ); $RxClip->fill("HEADLINE","COPY AND PASTE WEB DESIGN",$clipboard[0]); $RxClip->fill("CONTENT","Dynamically Create Page Templates And Render Them",$clipboard[1]); $RxClip->fillAll($plugs,$clipboard[2]); //Render The $clipboard Contents //Copy All Occurences Of Everything Between Two Strings Or Regular Expressions $RxClip->copy_between($data,"<a","</a>"); //returns all anchor tags in $data as a property of #RxClip->matches which is a //multi-dimensional array //Go From A Starting String/Regex Until Another String/Regex Then Capture A String/Regex Pattern //If you have several spans like this : <span class="ln">$115.95</span> //You can get the numericl value in them like this: $RxClip->go_until_capture("<span",'"ln"">$',"[1234567890]{3}.[123456789]{2}",$data); //The results are saved as a propery $RxClip->captures and would look like this : ( ( [0] => $102.99 [1] => 102.99 ) ( [0] => $110.98 [1] => 110.98 ) ( [0] => $109.99 [1] => 109.99 ) }
以上是关于正则表达式剪贴板类(迷你模板引擎)的主要内容,如果未能解决你的问题,请参考以下文章