微信小程序显示cms里的html文章
2018-02-11 10:56
441 查看
首先在cms模版中将html文章转化为json数据,识别图片,文本和换行,过滤掉样式和标签。这里是用PHP的正则表达式函数来实现的,$content是cms里的html文章。
小程序显示文章时请求cms生成的json数据,并通过循环和模版将文章内容显示出来。{{content}}是cms模版输出的json数据,是一条条段落或图片数据组成的数组。
<?php $_arr = preg_split('/(<img.*?>)/i', $content, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); $_r = array(); foreach($_arr as $_txt) { if(substr($_txt, 0, 4) == '<img') { $_matchs = array(); preg_match('/<img.*?src="(.*?)"/i', $_txt, $_matchs); $_txt = $_matchs[1]; if(preg_match('/^\//', $_txt)) $_txt = $gupload.$_txt; $_r[]= array('type'=>'img', 'data'=>$_txt); }else { $_txt = preg_replace('/&.*?;/', ' ', $_txt); $_txt = preg_replace('/\s+/', ' ', $_txt); $_txt = preg_replace(array('/<br.*?>/i', '/<p.*?>/i', '/<li.*?>/i', '/<div.*?>/i', '/<tr.*?>/i', '/<th.*?>/i'), "\n", $_txt); $_txt = preg_replace('/<.*?>/', '', $_txt); $_r[]= array('type'=>'txt', 'data'=>$_txt); } } $_data = array('title'=> $title, 'info'=> $inputtime, 'content'=> $_r); echo json_encode($_data); ?>
小程序显示文章时请求cms生成的json数据,并通过循环和模版将文章内容显示出来。{{content}}是cms模版输出的json数据,是一条条段落或图片数据组成的数组。
<template name="img"> <view> <image class="content-img" mode="aspectFit" src="{{item.data}}"></image> </view> </template> <template name="txt"> <view> <text>{{item.data}}</text> </view> </template> <view class="content"> <block wx:for="{{content}}"> <template is="{{item.type}}" data="{{item}}"/> </block> </view>
相关文章推荐
- 微信小程序显示cms里的html文章
- 微信小程序中显示html格式内容的方法
- 微信小程序Markdonw、HTML富文本内容显示解决办法
- 微信小程序中显示html格式内容的方法
- 微信小程序Markdonw、HTML富文本内容显示解决办法
- 微信小程序显示 html wepy + wxParse
- 微信小程序中显示HTML格式内容的实例
- 微信小程序image组件binderror使用例子(对应html、js中的onerror) ,图片失效显示默认图片
- 微信小程序开发背景图显示功能
- 小程序html转wxml,微信小程序用wxParse解析html
- 微信小程序-隐藏和显示自定义的导航
- html文章字段的隐藏和显示
- 微信小程序 —— 动态决定页面元素显示或隐藏的技巧
- 把文章里边的html标签去掉(去掉文字的样式,显示css设置的样式)
- 微信小程序获取微信公众号文章2
- 微信小程序文章界面简单布局
- 我看小程序系列文章:1 不一样的角度 解读微信小程序
- 截取html页面的文章内容显示前一部分内容
- 截取文章一部分显示(无损html)
- 微信小程序真机预览跟本地不同(轮播里面的商品图片不显示)