如何将HTML转换为降价? [英] How can I convert HTML to markdown?

查看:95
本文介绍了如何将HTML转换为降价?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像stackoverflow这样的网站.有一个文本区域,人们可以写下答案.我使用此PHP库来转换markdown.我的意思是我使用该函数将*italic*转换为<i>italic</i>.

I have a website like stackoverflow. There is a textarea which people write answers. I use this PHP library to convert markdown. I mean I use that function to convert *italic* to <i>italic</i>.

// include that library
$Parsedown = new Parsedown();
echo $Parsedown->text('*italic*'); # prints: <i>italic</i>

很好.应该注意,我将答案(转换为html标签,而不是markdown符号)转换为存储,换句话说,数据库中的所有数据都包含HTML标签,而不是markdown符号.

Well All fine. It should be noted I convert that answer (to html tags, not markdown symbols) befor storing, in other word, all data in the database are containing HTML tags, not markdown symbols.

现在,我想实现 Editing-System 来获取答案.完全类似于stackoverflow的东西.因此,我需要再次将保存的答案转换为降价样式.

Now I want to implement Editing-System for answers. Something exactly like stackoverflow. So I need to convert saved answer to markdown-style again.

现在,我想知道如何重新转换它?我的意思是我想将<i>italic</i>转换为*italic*,我该怎么做?

Now I want to know, how can I reconvert it? I mean I want to convert <i>italic</i> to *italic*, How can I do that?

推荐答案

而不是将原始输入存储为 parsed markdown,您应该存储实际的markdown并仅在将其渲染为HTML时将其转换为HTML.页面.

Rather than storing the original input as parsed markdown, you should store the actual markdown and convert it to HTML only when rendering it to the page.

通过这种方式,您可以保留原始的Markdown以便根据需要进行编辑.这也更加合乎逻辑.

This way you retain the original markdown for editing as required. It is also more logical.

这篇关于如何将HTML转换为降价?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆