php - 如何通过php跳过段落中的某些单词

标签 php javascript mysql html css

我在数据库中有一段是这样的

$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 字符串:

  1. SO question可能会有帮助。
  2. this one .
  3. 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();
});

Here's a fiddle to play with!

关于php - 如何通过php跳过段落中的某些单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11389049/

相关文章:

php - 如何使用 php 覆盖 Apache keep-alive header ?

php - 仅替换文件形式 preg_replace 中的特定组

javascript - 根据字符大小指定输入字段宽度

MySQL 查询相同兴趣

两个选择查询之间的 MySQL 差异

php - 清理多项选择列表的最佳方法

php - 如何在表单中的每个输入/新行之后添加 <br/> 标签

mysql - 如何在同一张表中使用 ORDER BY 更新表?

javascript - 音频标签的 Html5 惰性 'onplay' 事件处理程序?

javascript - 嵌套 Observables 在 Ionic2/Angular2 应用程序中表现不同