在PHP或JS中将html代码拆分为两个相同的内容部分

时间:2009-05-27 06:29:37

标签: php javascript html string split

我想获取网站文章的内容并创建两列或更多列文本。 困难的部分是我必须保留html标签,如果在<p></p>内进行切割,也必须关闭它们。

示例:

<p><span>One two three <strong>four</strong></span> five six</p>

结果:

<p><span>One two three<span><p>
<p><span><strong>four</strong> five six</p>

所以用户将会

One two three 
four five six

2 个答案:

答案 0 :(得分:3)

您必须在堆栈数据结构中跟踪已打开的标记。然后,当达到分割点时,必须按照确切的顺序关闭堆栈中的每个标记。对于下一个字符串中的openening标记(反向顺序)也是如此。

答案 1 :(得分:0)

如果您要使用javascript,我建议http://welcome.totheinter.net/columnizer-jquery-plugin/