最近苏州网站建设有个空闲时间,将之前使用的网站后台程序更换成了pbootcms,但是之前程序里设置的详情页面里上传的图片还想要保留之前一样的功能,就是将图片的标题设置为当前文章的标题。于是网上搜罗了一番,借鉴了别人已经开发好的程序,成功的实现了功能!
在项目文件里面找到ExtLabelController控制器,文件所在位置:/apps/home/controller/ExtLabelController.php
{pboot:pre}
namespace apphomecontroller; use apphomemodelParserModel; use coreasicController; class ExtLabelController extends Controller { protected $content; protected $model; public function __construct(){ $this->model = new ParserModel(); } /* 必备启动函数 */ public function run($content) { // 接收数据 $this->content = $content; // 执行个人自定义标签函数 $this->test(); $this->siteImgTitle(); // 返回数据 return $this->content; } // 测试扩展单个标签 private function test() { $this->content = str_replace('{pboot:userip}', get_user_ip(), $this->content); } private function siteImgTitle(){ $pattern = '/{setimgtitles?(([^}]+))}/'; $pattern_title = '/@(title=)["|'](.*?)["|'](.*?)/'; if (preg_match($pattern, $this->content, $matches)) { $this->content = preg_replace_callback( $pattern, function($matches) use ($pattern_title) { // print_r($matches[1]); $title = ''; $str = $matches[1]; if(preg_match($pattern_title,$str,$title_matches)){ // var_dump($title_matches); $title = $title_matches[2]; $str = preg_replace($pattern_title,' ',$str); } // var_dump($str); // return set_img_title($matches[1],$title); return set_img_title($str,$title); }, $this->content ); } // $this->content = set_img_title($this->content,123); } }
{/pboot:pre}
在项目文件夹中找到以下/apps/common/function.php文件,并在结尾出增加以下代码:
{pboot:pre}
function set_img_title($content,$title=''){ $reg = '/<s*imgs+[^>]*?srcs*=s*('|")(.*?)[^>]*?/?s*>/i'; // preg_match($reg,$content,$match); if(empty($title)){ $info = preg_replace($reg,'<img src="${2}" />',$content); }else{ $info = preg_replace($reg,'<img src="${2}" title="'.$title.'" alt="'.$title.'" />',$content); } return $info; }
{/pboot:pre}
接着修改/apps/home/controller/ParserController.php大约在50行的如下代码:
{pboot:pre}
// 解析个人扩展标签,升级不覆盖 if (file_exists(APP_PATH . '/home/controller/ExtLabelController.php')) { if (class_exists('apphomecontrollerExtLabelController')) { $extlabel = new ExtLabelController(); $content = $extlabel->run($content); } }
{/pboot:pre}
移动到该方法的最下面,也就是在该方法的:
{pboot:pre}
return $content;
{/pboot:pre}
之前,这个代码之前。
最终方法如下:
{pboot:pre}
// 解析全局后置公共标签 public function parserAfter($content) { // 默认页面信息替换 $content = str_replace('{pboot:pagetitle}', $this->config('other_title') ?: '{pboot:sitetitle}-{pboot:sitesubtitle}', $content); $content = str_replace('{pboot:pagekeywords}', '{pboot:sitekeywords}', $content); $content = str_replace('{pboot:pagedescription}', '{pboot:sitedescription}', $content); $content = str_replace('{pboot:keyword}', get('keyword', 'vars'), $content); // 当前搜索的关键字 $content = $this->parserSiteLabel($content); // 站点标签 $content = $this->parserCompanyLabel($content); // 公司标签 $content = $this->parserMemberLabel($content); // 会员标签 $content = $this->parserNavLabel($content); // 分类列表 $content = $this->parserSelectAllLabel($content); // CMS筛选全部标签解析 $content = $this->parserSelectLabel($content); // CMS筛选标签解析 $content = $this->parserSpecifySortLabel($content); // 指定分类 $content = $this->parserListLabel($content); // 指定列表 $content = $this->parserSpecifyContentLabel($content); // 指定内容 $content = $this->parserContentPicsLabel($content); // 内容多图 $content = $this->parserContentCheckboxLabel($content); // 内容多选调取 $content = $this->parserContentTagsLabel($content); // 内容tags调取 $content = $this->parserSlideLabel($content); // 幻灯片 $content = $this->parserLinkLabel($content); // 友情链接 $content = $this->parserMessageLabel($content); // 留言板 $content = $this->parserFormLabel($content); // 自定义表单 $content = $this->parserSubmitFormLabel($content); // 自定义表单提交 $content = $this->parserSqlListLabel($content); // 自定义SQL输出 $content = $this->parserQrcodeLabel($content); // 二维码生成 $content = $this->parserPageLabel($content); // CMS分页标签解析(需置后) $content = $this->parserIfLabel($content); // IF语句(需置最后) $content = $this->parserLoopLabel($content); // LOOP语句(需置后,不可放到if前面,否则有安全风险) $content = $this->restorePreLabel($content); // 还原不需要解析的内容 $content = $this->parserReplaceKeyword($content); // 页面关键词替换 // 解析个人扩展标签,升级不覆盖 if (file_exists(APP_PATH . '/home/controller/ExtLabelController.php')) { if (class_exists('apphomecontrollerExtLabelController')) { $extlabel = new ExtLabelController(); $content = $extlabel->run($content); } } return $content; }
{/pboot:pre}
为什么要这么做?因为实际开发过程中,会需要对后台输出的字段进行二次处理,那么如果扩展控制器放在前面处理,就会发生无法获取后台字段的情况。
那么我们只要将扩展控制器放到最后来执行,就可以啦。
最后我们只需要在文章详情页面调用以下代码就可以实现最终的效果了:
{pboot:pre}
{setimgtitle({content:content} @title='{content:title}')}
{/pboot:pre}
以上代码是自己整理修改,若有瑕疵请多多见谅,如有修改意见和问题给我留言!