2010-08-30 67 views
0

我有一个包含多个H1标题的页面,后面跟着文本等。 实施例:PHP获取多个文字<h1>...</h1>标签

<h1>Title 1</h1>Some text here 
<h1>Title 2</h1>Some more text here 
<h1>Title 3</h1>Even more text here 

我想要做的就是创建元素的数组,即爆炸使用如上,我在一个$输出变量隔板<h1>ANY TEXT</h1>的HTML。

最终目的是要算结束</h1>和下一次起动<h1>之间的文本strlen的,如果它超过200个字符更高隐藏它<span>display:none内部,以便用户可以按“全部显示”取消隐藏。

我该如何得到它?

+2

听起来像客户端JavaScript将帮助你在这里。 – 2010-08-30 13:19:47

+2

[你应该删除正则表达式并使用解析器代替](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) – Amarghosh 2010-08-30 13:21:11

+1

给他们一个ID和使用getelementbyid() – Grumpy 2010-08-30 13:27:15

回答

1

您可以使用SimplePHPDom做到这一点发送页面到客户端之前:

ob_start(); 
// build page here 
$html = ob_get_clean(); 

$dom = str_get_html($html); 

$headings = $dom->find('h1'); 

foreach($headings as $h1) { 
    // process node to add CSS to hide node and change text to 'show more' 
} 

这也可以在客户端完成与jQuery/MooTools的,具有基本相同的过程(减去缓冲捕捉东西)。

+0

我想OP想分析**后面的文本**来自H1标签......'文本在结尾和下一个开始

' – 2010-08-30 16:02:14

+0

啊是的... D'哦...星期一......在这种情况下,'$ h1-> nextSibling()'可能会在'foreach() – 2010-08-30 16:40:30