这个PHP正则表达式有什么作用?

时间:2013-08-12 21:52:03

标签: php regex

public function getBlock( $tag )
   {
     preg_match ('#<!-- START '. $tag . ' -->(.+?)
         <!-- END '. $tag . ' -->#si', $this->content, $tor);
     $tor = str_replace ('<!-- START '. $tag . ' -->', "", $tor[0]);
     $tor = str_replace ('<!-- END '  . $tag . ' -->', "", $tor);
     return $tor;
}

有谁知道这个功能是做什么的?

2 个答案:

答案 0 :(得分:2)

看起来会删除

'#<!-- START '. $tag . ' -->'

'<!-- END '  . $tag . ' -->'

来自$ this-&gt;内容中的文字,并返回这些评论之间的所有文字。

答案 1 :(得分:2)

public function getBlock( $tag )
   {
     //below finds the start tag, then matches any character multiple times
     // until it finds <!-- END $tag -->, store the result in $tor
     preg_match ('#<!-- START '. $tag . ' -->(.+?)
         <!-- END '. $tag . ' -->#si', $this->content, $tor);

     //the # is the delimiter, with s meaning treat as a single line 
     // so . matches \r\n for example. and i means insensitive 
     $tor = str_replace ('<!-- START '. $tag . ' -->', "", $tor[0]);
     $tor = str_replace ('<!-- END '  . $tag . ' -->', "", $tor);

     //remove the line with start on then remove line with end on.

     return $tor;
     //return what was between the two lines.
}

我已经对该功能添加了一些评论,希望他们能够更清楚地说明