如何将HTML插入到PHP DOM对象中? [英] How do I insert HTML into a PHP DOM object?

查看:275
本文介绍了如何将HTML插入到PHP DOM对象中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP的DOM对象为我的网站创建HTML页面。这对我的头很好,但是由于我将进入大量的HTML(不通过DOM),我认为我需要使用 DOM-> createElement($ bodyHTML)将我的HTML从我的网站添加到DOM对象。

I am using PHP's DOM object to create HTML pages for my website. This works great for my head, however since I will be entering a lot of HTML into the body (not via DOM), I would think I would need to use DOM->createElement($bodyHTML) to add my HTML from my site to the DOM object.

然而, DOM-> createElement 似乎解析了所有的HTML实体,所以我的最终结果最终显示了HTML该页面而不是实际呈现HTML。

However DOM->createElement seems to parse all HTML entities so my end result ended up displaying the HTML on the page and not the actual renders HTML.

我目前正在使用黑客来使其正常工作,

I am currently using a hack to get this to work,

$body = $this->DOM
             ->createComment('DOM Glitch--><body>'.$bodyHTML."</body><!--Woot");

其中所有我的网站代码都在一个评论中,我绕过了评论,并手动添加< body> 标签。

Which puts all my site code in a comment, which I bypass athe comment and manually add the <body> tags.

目前,这种方法有效,但我认为应该有更合适的方法。理想的是像 DOM-> createElement(),不会解析任何字符串。

Currently this method works, but I believe there should be a more proper way of doing this. Ideally something like DOM->createElement() that will not parse any of the string.

我也试过使用 DOM-> createDocumentFragment()然而,它不喜欢某些字符串,所以它会出错并且不起作用(除了占用额外的CPU电源来重新解析身体的HTML)。

I also tried using DOM->createDocumentFragment() However it does not like some of the string so it would error and not work (Along with take up extra CPU power to re-parse the body's HTML).

所以,我的问题是,有没有更好的方法来做,除了使用 DOM-> createComment() / code>?

So, my question is, is there a better way of doing this other than using DOM->createComment()?

推荐答案

我花了很多时间在Anthony Forloney的答案上工作,但我似乎不能html附加到身体没有它错误。

I spent a lot of time working on Anthony Forloney's answer, But I cannot seem to get the html to append to the body without it erroring.

@Mark B:我已经尝试这样做,但正如我在评论中说的,它在我的html上错误。

@Mark B: I have tried doing that, but as I said in the comments, it errored on my html.

我忘了添加以下内容,我的解决方案:

I forgot to add the below, my solution:

我决定让我的html对象更简单,允许我通过不使用DOM并使用字符串来执行此操作。

I decided to make my html object much simpler and to allow me to do this by not using DOM and just use strings.

这篇关于如何将HTML插入到PHP DOM对象中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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