1

我有一个段落存储在数据库中,我只想从中获取前五行。它怎么做?

我应该将第一个数组转换为 srting 吗?如果是,那么该怎么做?

如果它的字符串比我可以做到这一点

$str='mayank kumar swami mayank kumar swami';
$var= strlen($str);
for($i=0;$i<8;$i++){
    echo $str[$i];
}
  1. 或者如何通过 sql 从数据库中只获取 200 字?

我知道它可以通过 css 轻松显示在 Show first line of a paragraph nut 我想通过 php 或 sql 查询来完成

我在做什么

$article_result = mysql_query("SELECT * FROM article ORDER BY time DESC LIMIT 1",$connection);
    if($article_result){
        while($row = mysql_fetch_array($article_result)) 
            {

                echo "<div class=\"article_div\" >";
                echo "<h4 id=\"article_heading\"><img src=\"images/new.png\" alt=\"havent got\" style=\"padding-right:7px;\">".$row['article_name']."</h4>";
                echo"<h5 class=\"article_byline\">";
                echo" by";
                echo"<a href=\"#\">{$row['authore']}</a></h5>";
                echo" <div id=\"article_about\"><p>{$row['content']}</p></div>";
                             //here i want to get only 2000  word from database (content)

                echo "</div>";
            }
        }
4

5 回答 5

5

这个问题有很多解决方案。

如果您想按字数拆分它,类似于user247245发布的内容:

function get_x_words($string,$x=200) {
  $parts = explode(' ',$string);
  if (sizeof($parts)>$x) {
    $parts = array_slice($parts,0,$x);
  }
  echo implode(' ',$parts);
}

然而,我首选的方法是获取所有完整的单词直到某个点(例如 200 个字符):

function chop_string($string,$x=200) {
  $string = strip_tags(stripslashes($string)); // convert to plaintext
  return substr($string, 0, strpos(wordwrap($string, $x), "\n"));
}

以上会将字符串截断为 200 个字符,但只会在单词结尾后截断(所以最后不会返回半个单词)

于 2012-05-25T08:35:29.360 回答
3

我们是在说单词、线条还是字母?

如果的话:

$a = explode(' ',$theText);
if (sizeof($a)>200) $a = array_slice($a,0,200);
echo implode(' ',$a);

问候,

于 2012-05-25T08:18:34.177 回答
2

您可以在mysql中使用子字符串函数

SELECT SUBSTRING('Quadratically',1,5);

返回

Quadr

我建议您使用 sql,因为它减少了数据库服务器和应用程序服务器之间的数据传输量。

所以,现在你修改这个

$article_result = mysql_query("SELECT article_name, authore, SUBSTRING(content,1,200) as content FROM article ORDER BY time DESC LIMIT 1",$connection);
于 2012-05-25T08:01:12.533 回答
2

尝试这个:

<?php
echo  substr("mayank kumar swami mayank kumar swami", 0, 6);
?>

结果输出:mayank

于 2012-05-25T08:05:38.270 回答
0
<?php
$about_vendor ="Lorem ipsum dolor sit amet, consectetur adipisicing elit. Adipisci officia excepturi quisquam mollitia, obcaecati cupiditate, quaerat est quibusdam nostrum esse culpa voluptates eum, et architecto animi. Voluptates enim tenetur minus! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Laboriosam magni exercitationem non at error possimus, voluptas aut, aperiam sint pariatur illo libero vel aspernatur tempora laborum. Harum nesciunt quos at. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Vitae quidem saepe voluptates minus delectus, dolores, repellat maiores quae consectetur quasi qui voluptas eius odit autem optio cupiditate nesciunt iste ducimus!"
// Convert string to array
$convert_to_array = explode(' ',$about_vendor);
// Total length of array
$total_length_of_array =  count($convert_to_array);
?>
<p class="tip_par" style="text-align:justify;">
<!-- specify how many words do you want to show, I like to show first 30 words -->
  <?php for($i=0;$i<=30;$i++) {
  echo $convert_to_array[$i].' ';
  } ?>
  <!-- If you have more than 30 word it will show on toggle click on below more link -->
  <span style="color:#C1151B;">  <span data-toggle="collapse" data-target="#demo" style="cursor:pointer;"><?php if($total_length_of_array >30) {
    echo "more";
  } ?></span>
  <div id="demo" class="collapse tip_par" style="padding-top: 0px;">
    <?php  for($i=31;$i<$total_length_of_array;$i++) {
    echo $convert_to_array[$i].' ';
    } ?>
  </div>
</span> </p>
于 2016-03-19T11:09:01.977 回答