0

我正在使用 TinyMCE 将内容发布到我的网站。我有一个问题,即使我将光标放在内容的末尾,我也只能在另一个元素(例如段落)中插入图像。

因此,当我发布内容时,我目前最终会得到如下标记:

<p>Text content <img src="blah" /></p><p>Another paragraph</p>

我注意到 WordPress 和 TinyMCE 示例站点都以上述方式插入图像。

我一直无法找到解决方案来解决如何在 TinyMCE 中将图像插入到任何其他元素之外,因此下一步是尝试更改内容服务器端,以便在发布内容时能够更正将标记保存到我的数据库之前,这样我最终会得到:

<p>Text content</p><img src="blah" /><p>Another paragraph</p>

有没有人知道我需要做什么才能实现这一目标。我知道正则表达式应该可以工作,但我不确定什么或如何使用它来达到这种效果。更好的是,如果有人能解决 TinyMCE 问题,那就更好了。

我知道我显然可以使用 JS 来即时实现这个客户端,但这种解决方案并不理想。

非常感谢,

D

4

1 回答 1

2

Don't use RegEx for manipulating HTML nodes! It's not designed for it and is too fragile.

HTML is not a Regular language, and even with the non-Regular features many modern Regular Expression implementations support, trying to manipulate HTML with a regex is still difficult at best.


For a quick-and-dirty solution, you can do:

preg_replace
    ( "/(<img[^>]++>)\s*+(</p>)/gi"
    , "$2$1"
    , $HtmlText
    );


But that will fail with several valid HTML constructs - what you should actually be using is something like PHP Simple HTML DOM Parser to parse the HTML text into a HTML DOM, manipulate that DOM, and then output as text again.

于 2009-11-01T15:07:45.527 回答