1

我在我的网站上使用 tinyMCE 作为我的文本编辑器,我想在将文本保存到我的数据库之前重新格式化文本(将 ’ 标签更改为 ' 然后再更改为 ')。我找不到使用 tinyMCe 执行此操作的简单方法,并且使用 htmlentities() 会更改包括 <> 在内的所有内容。有任何想法吗?

4

5 回答 5

8

您可以strip_tags($str, $allowed_tags)像下面这样使用:

$txt = strip_tags($txt, '<p><a><br>');
于 2008-10-14T12:31:48.027 回答
1

tinyMCE allows you to specify a 'whitelist' of allowed tags, which will remove any tags not included on the list:

tinyMCE.init({
  ...  //  other init instructions
  valid_elements: 'p,a[href],br',
});

In our own project we combine this whitelist with an internal converter which turns the HTML into a BB-like format for the database, then back to HTML again when it needs to be printed to a page.


Update: Now that the question has been edited to be clearer, I can see that what I typed above doesn't solve the problem. What the questioner wants is a way to convert character entities while leaving HTML tags unaffected.

In our own project, the internal converter we use does this job. When converting from HTML into our internal representation, encoded characters are converted into the characters themselves; when converting back into HTML, higher characters are encoded. This is done in a character-by-character, parser-like style. However this approach is probably too complicated for your needs.

The shortcut used by many is to use a series of regular expressions, but you may find it difficult to arrange your regexes in such a way as to preserve ampersands & and semicolons ; at the same time as translating character entities &nbsp;. You'll also find that to cover every possible character entity you'd need dozens of regexes.

Uh, so I don't actually have an answer.

于 2008-10-14T13:30:43.087 回答
1

TinyMCE 和 FCK 都有大量的配置选项。文档可能很难搜索,但值得付出努力。

TinyMCE 允许您使用 'entity_encoding' 选项指定实体编码。可以在创建编辑器时指定。它可能看起来像这样......

tinyMCE.init({
    entity_encoding: 'numeric'
});

这会改变像 ’ 这样的标签。进入 '。

于 2008-10-15T00:42:39.113 回答
1

直接来自 PHP 手册:strip_tags()

$allowable_tags 变量允许您定义一串允许的标签。您可以使用此可选的第二个参数来指定不应剥离的标签。

于 2008-10-14T12:48:31.223 回答
1

这取决于您要保留的标签。我假设您想使用 TinyMCE 的所有功能,因此文本可以包含诸如表格结构之类的下一个标签。那么就没有简单的方法了(一种方法是使用PHP 文档对象模型来解析 html 文档。

但是 TinyMCE 有几个用于实体编码的配置选项。我建议您查看 TinyMCE 手册中的配置选项entity_encoding实体编码

于 2008-10-14T13:56:16.593 回答