uniapp解析富文本(包含图片)微信小程序+app可用

 最终效果图:

1、uniapp中,在需要使用的vue界面中

<template>
	<view class="box">
		<view class="title">{{title}}</view>
		<rich-text :nodes="content" class="content"></rich-text> 
	</view>
</template>

<script>
	
	export default {
		data() {
			return {
				id:'',
				title:'',
				content:'',
				flag:0
			}
		},
		methods: {
			
			getdetai(){
				uni.request({
					url:this.SiteUrl+'getDetail',
					data:{id:this.id},
					method:'GET',
					success: (res) => {
						//有数据
						if(res.data.code==1){
							this.title=res.data.res.title;
							//富文本使用
							this.content=this.formatRichText(res.data.res.content);
						}
						if(res.data.code==0){
							this.flag=1;
						}
					},
					fail: (err) => {
						console.log(err)
					}
					
				})
			},
			//解析富文本方法
			formatRichText(html) {
					let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
						match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
						match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
						match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
						return match;
					});
					newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
						match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
						return match;
					});
					newContent = newContent.replace(/<br[^>]*\/>/gi, '');
					newContent = newContent.replace(/\<img/gi,
						'<img style="max-width:100%;height:auto;display:inline-block;margin:10rpx auto;"');
					return newContent;
				}
			
		},

		onLoad(option) {
			this.id=option.id;
			this.getdetai();
			
		}
	}
</script>


 2、在后端php中,将提交保存到数据库的富文本图片路径更改为含http或https的(含自己域名的),不然图片无法展示。

<img src="upload/article/2022/04-03/273e82bf525920976b0aa5d845a59cb9.jpg" alt="" style="display:inline-block;height:auto;max-width:100%">

 改为:

<img src="http://test.com/upload/article/2022/04-03/273e82bf525920976b0aa5d845a59cb9.jpg" alt="" style="display:inline-block;height:auto;max-width:100%">

方法如下:

 //替换文本图片中的路径
             $url=getUrl();
               $list['content'] = str_replace('max-width:50%', 'max-width:100%' , $list['content']);//更改图片展示大小
              $list['content'] = str_replace('src="/', 'src="'.$url.'/' , $list['content']);//更改图片路径

用得到的获取url方法 (得到如:http://www.baidu,com)

/**
 * ThinkPHP 获取url
 * @return string
 */
function getUrl() {
    $sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
    $php_self = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
    $path_info = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
    return $sys_protocal.(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '');
}

 通过uniapp使用uparse发现微信小程序端图片能够正常展示,但是APP图片无法展示,所以使用这种方法。

  • 5
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值