阅读更多[仅允许两个p标签]

时间:2013-10-28 05:36:44

标签: php

我正在为自己创建一个简单的博客脚本,并且我在某处停留。

如何只显示2个p标签?

我的文字:

<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>

如何让它只返回前两段?

像这样:

<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>

2 个答案:

答案 0 :(得分:1)

快速而肮脏解决方案。首先它会检查是否有超过2个</p>标签。如果是 - 它将只回显其中2个+阅读更多链接。否则原始字符串将被打印而没有链接。

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

if (count($aText) > 2)
{
    echo $aText[0].'</p>'.$aText[1].'</p>';
    echo '<a href="">Read more</a>';
}
else
{
    echo $str;
}

答案 1 :(得分: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>