preg_replace_callback 怎么替换preg_replace
2个回答
展开全部
自问自答~~~~~~~~~~~~~~thinkphp3.2 git代码,看下官方怎么写.......
/**
* TagLib库解析
* access public
* @param string $tagLib 要解析的标签库
* @param string $content 要解析的模板内容
* @param boolen $hide 是否隐藏标签库前缀
* return string
*/
public function parseTagLib($tagLib,&$content,$hide=false) {
$begin = $this->config['taglib_begin'];
$end = $this->config['taglib_end'];
$className = 'TagLib'.ucwords($tagLib);
$tLib = Think::instance($className);
$that = $this;
foreach ($tLib->getTags() as $name=>$val){
$tags = array($name);
if(isset($val['alias'])) {// 别名设置
$tags = explode(',',$val['alias']);
$tags[] = $name;
}
$level = isset($val['level'])?$val['level']:1;
$closeTag = isset($val['close'])?$val['close']:true;
foreach ($tags as $tag){
$parseTag = !$hide? $tagLib.':'.$tag: $tag;// 实际要解析的标签名称
if(!method_exists($tLib,'_'.$tag)) {
// 别名可以无需定义解析方法
$tag = $name;
}
$n1 = empty($val['attr'])?'(\s*?)':'\s([^'.$end.']*)';
$this->tempVar = array($tagLib, $tag);
if (!$closeTag){
if ( version_compare(PHP_VERSION,'5.3.0')<0 ) {
$patterns = '/'.$begin.$parseTag.$n1.'\/(\s*?)'.$end.'/eis';
$replacement = "\$this->parseXmlTag('$tagLib','$tag','$1','')";
$content = preg_replace($patterns, $replacement,$content);
}else{
$patterns = '/'.$begin.$parseTag.$n1.'\/(\s*?)'.$end.'/is';
$content = preg_replace_callback($patterns, function($matches) use($tagLib,$tag,$that){
return $that->parseXmlTag($tagLib,$tag,$matches[1],$matches[2]);
},$content);
}
}else{
if ( version_compare(PHP_VERSION,'5.3.0')<0 ) {
$patterns = '/'.$begin.$parseTag.$n1.$end.'(.*?)'.$begin.'\/'.$parseTag.'(\s*?)'.$end.'/eis';
$replacement = "\$this->parseXmlTag('$tagLib','$tag','$1','$2')";
for($i=0;$i<$level;$i++)
$content=preg_replace($patterns,$replacement,$content);
}else{
$patterns = '/'.$begin.$parseTag.$n1.$end.'(.*?)'.$begin.'\/'.$parseTag.'(\s*?)'.$end.'/is';
for($i=0;$i<$level;$i++) {
$content=preg_replace_callback($patterns,function($matches) use($tagLib,$tag,$that){
return $that->parseXmlTag($tagLib,$tag,$matches[1],$matches[2]);
},$content);
}
}
}
}
}
}
/**
* TagLib库解析
* access public
* @param string $tagLib 要解析的标签库
* @param string $content 要解析的模板内容
* @param boolen $hide 是否隐藏标签库前缀
* return string
*/
public function parseTagLib($tagLib,&$content,$hide=false) {
$begin = $this->config['taglib_begin'];
$end = $this->config['taglib_end'];
$className = 'TagLib'.ucwords($tagLib);
$tLib = Think::instance($className);
$that = $this;
foreach ($tLib->getTags() as $name=>$val){
$tags = array($name);
if(isset($val['alias'])) {// 别名设置
$tags = explode(',',$val['alias']);
$tags[] = $name;
}
$level = isset($val['level'])?$val['level']:1;
$closeTag = isset($val['close'])?$val['close']:true;
foreach ($tags as $tag){
$parseTag = !$hide? $tagLib.':'.$tag: $tag;// 实际要解析的标签名称
if(!method_exists($tLib,'_'.$tag)) {
// 别名可以无需定义解析方法
$tag = $name;
}
$n1 = empty($val['attr'])?'(\s*?)':'\s([^'.$end.']*)';
$this->tempVar = array($tagLib, $tag);
if (!$closeTag){
if ( version_compare(PHP_VERSION,'5.3.0')<0 ) {
$patterns = '/'.$begin.$parseTag.$n1.'\/(\s*?)'.$end.'/eis';
$replacement = "\$this->parseXmlTag('$tagLib','$tag','$1','')";
$content = preg_replace($patterns, $replacement,$content);
}else{
$patterns = '/'.$begin.$parseTag.$n1.'\/(\s*?)'.$end.'/is';
$content = preg_replace_callback($patterns, function($matches) use($tagLib,$tag,$that){
return $that->parseXmlTag($tagLib,$tag,$matches[1],$matches[2]);
},$content);
}
}else{
if ( version_compare(PHP_VERSION,'5.3.0')<0 ) {
$patterns = '/'.$begin.$parseTag.$n1.$end.'(.*?)'.$begin.'\/'.$parseTag.'(\s*?)'.$end.'/eis';
$replacement = "\$this->parseXmlTag('$tagLib','$tag','$1','$2')";
for($i=0;$i<$level;$i++)
$content=preg_replace($patterns,$replacement,$content);
}else{
$patterns = '/'.$begin.$parseTag.$n1.$end.'(.*?)'.$begin.'\/'.$parseTag.'(\s*?)'.$end.'/is';
for($i=0;$i<$level;$i++) {
$content=preg_replace_callback($patterns,function($matches) use($tagLib,$tag,$that){
return $that->parseXmlTag($tagLib,$tag,$matches[1],$matches[2]);
},$content);
}
}
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用模式e
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询