-1

我正在为自己创建一个简单的博客脚本,但我卡在了某个地方。

我怎样才能只显示 2 p 标签?

IE

我的文字:

<p>Some text, article here. this is the first paragraph</p>
<p>Some text, article here. this is the second paragraph</p>
<p>Some text, article here. this is the third paragraph</p>
<p>Some text, article here. this is the fourth paragraph</p>

我怎样才能让它只返回前 2 段?

像这样:

<p>Some text, article here. this is the first paragraph</p>
<p>Some text, article here. this is the second paragraph</p>
<a href="dont-worry-about-the-link">Read More</a>
4

2 回答 2

1

Fast and dirty solution. First of it will check it there is more than 2 </p> tags. If yes - it will echo only 2 of them + Read more link. Otherwise original string will be printed without link.

$aText = explode('</p>', $str);

if (count($aText) > 2)
{
    echo $aText[0].'</p>'.$aText[1].'</p>';
    echo '<a href="">Read more</a>';
}
else
{
    echo $str;
}
于 2013-10-28T06:36:05.063 回答
0

给所有的类名

标签和使用jquery如下:

<p class="testClass">Some text, article here. this is the first paragraph</p>
<p class="testClass">Some text, article here. this is the second paragraph</p>
<script>
$(document.ready(function() {
 var count = $('.testClass').length();
 if(count > 2) {
  // append 2 <tags> with readmore link
 }
}))
</script>
于 2013-10-28T05:55:03.643 回答