我在数据库中有一段是这样的
$str ="this is a paragraph i show shortly and when i click on the view more it will show completely for that i am using the ajax and retrieve it "
我这样显示
this is a paragraph i show shortly
php 显示第一个单词是
function chop_string($str, $x)// i called the function
{
$string = strip_tags(stripslashes($string));
return substr($string, 0, strpos(wordwrap($string, $x), "\n"));
}
当用户点击 view more
时,它会显示其余部分,但问题是如何跳过这个 this is a paragraph i show shortly
并显示剩下的
我想在点击 view more
时显示 $x
之后的段落
最佳答案
按单词数量的字符 chop 字符串:
- 这SO question可能会有帮助。
- 会this one .
- Here's some code这是专门通过字数计算的。
就单击链接时显示更多文本而言,我的建议是一次性从数据库中加载字符串并格式化其输出。如果您的字符串是:
This is whole string pulled from my database.
那么下面的代码将被格式化成这样:
HTML
<p class="truncate">This is the whole string <a class="showMore">Show more...</a><span> pulled from my database.</span></p>
CSS
p.truncate span { display: none; }
这样您就可以使用 Javascript(最好通过我为下面的代码选择的 jQuery 这样的库)来隐藏或显示您的解决方案的更多内容,而无需使用 AJAX 发出第二个数据库请求。以下 Javascript 将执行您的请求:
$("a.showMore").on("click", function() {
$(this).parent().find("span").contents().unwrap();
$(this).remove();
});
关于php - 如何通过php跳过段落中的某些单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11389049/