Other/其他

PbootCMS 自定义标签开发替换标题中的特殊符号

阿里云

最新在使用 PbootCMS 进行建站,现有的标签不符合自己的一些业务需求,想着自己做个符合自己的业务的标签,于是参考了一下网上的资料以后便开始着手开发了,整个二开还是比较简单,下面放出来需要修改的文件。

我的需求其实是在 news.HTML 也就是新闻中文章末尾添加一个自定义图片(根据标题生成一张图片,因为生成图片的方法不能包含特殊符号所以需要把标题中的特殊符号尤其是?去掉)

也想出现在这里?联系我们
创客主机
  1. <!-- 新增图片_dcx-->	
  2. <p style="text-align: center;" >
  3.        <img style="width:450px;" src="http://img.mybancai.cn/{pboot:titlereplace title='{content:title}'}.png" alt="{pboot:titlereplace title='{content:title}'}" >
  4. </p>

其中{pboot:titlereplace title='{content:title}'}便是我要新增的标签,title 这个属性是在下面的 parserTitleReplaceLabel 方法中进行 case 判断用的

section

1)在 parserAfter 方法中新增一行解析入口

$content = $this->parserTitleReplaceLabel($content);

  1. public function parserAfter($content)
  2.     {
  3.         // 默认页面信息替换
  4.         $content = str_replace('{pboot:pagetitle}', $this->config('other_title') ?: '{pboot:sitetitle}-{pboot:sitesubtitle}', $content);
  5.         $content = str_replace('{pboot:pagekeywords}', '{pboot:sitekeywords}', $content);
  6.         $content = str_replace('{pboot:pagedescription}', '{pboot:sitedescription}', $content);
  7.         $content = str_replace('{pboot:keyword}', get('keyword', 'vars'), $content); // 当前搜索的关键字
  8.  
  9.         // 解析个人扩展标签,升级不覆盖
  10.         if (file_exists(APP_PATH . '/home/controller/ExtLabelController.php')) {
  11.             if (class_exists('app\home\controller\ExtLabelController')) {
  12.                 $extlabel = new ExtLabelController();
  13.                 $content = $extlabel->run($content);
  14.             }
  15.         }
  16.  
  17.         $content = $this->parserSiteLabel($content); // 站点标签
  18.         $content = $this->parserCompanyLabel($content); // 公司标签
  19.         $content = $this->parserMemberLabel($content); // 会员标签
  20.         $content = $this->parserNavLabel($content); // 分类列表
  21.         $content = $this->parserSelectAllLabel($content); // CMS筛选全部标签解析
  22.         $content = $this->parserSelectLabel($content); // CMS筛选标签解析
  23.         $content = $this->parserSpecifySortLabel($content); // 指定分类
  24.         $content = $this->parserListLabel($content); // 指定列表
  25.         $content = $this->parserSpecifyContentLabel($content); // 指定内容
  26.         $content = $this->parserContentPicsLabel($content); // 内容多图
  27.         $content = $this->parserContentCheckboxLabel($content); // 内容多选调取
  28.         $content = $this->parserContentTagsLabel($content); // 内容tags调取
  29.         $content = $this->parserSlideLabel($content); // 幻灯片
  30.         $content = $this->parserLinkLabel($content); // 友情链接
  31.         $content = $this->parserMessageLabel($content); // 留言板
  32.         $content = $this->parserFormLabel($content); // 自定义表单
  33.         $content = $this->parserSubmitFormLabel($content); // 自定义表单提交
  34.         $content = $this->parserSqlListLabel($content); // 自定义SQL输出
  35.  
  36.         $content = $this->parserQrcodeLabel($content); // 二维码生成
  37.         $content = $this->parserPageLabel($content); // CMS分页标签解析(需置后)
  38.         $content = $this->parserIfLabel($content); // IF语句(需置最后)
  39.         $content = $this->parserLoopLabel($content); // LOOP语句(需置后,不可放到if前面,否则有安全风险)
  40.         $content = $this->restorePreLabel($content); // 还原不需要解析的内容
  41.         $content = $this->parserReplaceKeyword($content); // 页面关键词替换
  42.         $content = $this->parserTitleReplaceLabel($content); // 通用内容替换标签  这里是我新增的
  43.         return $content;
  44.     }

2)添加对应的解析方法

  1. // 通用内容替换标签 @mk-title_replace
  2.     //$content这里用的时文章标题,直接是字符串
  3.     public function parserTitleReplaceLabel($content)
  4.     {
  5.         $pattern = '/\{pboot:titlereplace(\s+[^}]+)?\}/';
  6.  
  7.         if (preg_match_all($pattern, $content, $matches)) {
  8.             $count = count($matches[0]);
  9.             for ($i = 0; $i < $count; $i ++) {
  10.                 $params = $this->parserParam($matches[0][$i]);
  11.                 $data = '';
  12.                 foreach ($params as $key => $value) {
  13.                     switch ($key) {
  14.                         case 'title'://这里其实可以解析很多的,不只是title,可以根据case进行不同的解析
  15.                             $data = $value; // 获取到的文章title
  16.  
  17.                             if (! $data) { // 无内容不解析
  18.                                 continue;
  19.                             }
  20.                             $data = titlereplace($data); //testreplace方法为自定义方法,在\apps\common\function.php里
  21.                             break;
  22.                     }
  23.                 }
  24.                 $content = str_replace($matches[0][$i], $data, $content);
  25.             }
  26.         }
  27.         return $content;
  28.     }

section

function.php 替换方法 titlereplace()在这里写

  1.     function titlereplace($data){
  2.         //把? ?都替换为空格
  3.         $search = array('?','?',' ','%',',');
  4.         return str_replace($search, "_", $data);
  5.     }

PbootCMS 自定义标签开发替换标题中的特殊符号

已有 277 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!