如何从段落中获得第一行

时间:2012-05-25 07:58:08

标签: php sql

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

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

如果它的字符串比我可以通过

执行此操作
$str='mayank kumar swami mayank kumar swami';
$var= strlen($str);
for($i=0;$i<8;$i++){
    echo $str[$i];
}
  1. 或者如何通过sql从数据库中获取200个单词?
  2. 我知道可以通过css轻松地在Show first line of a paragraph中显示我想通过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>";
                }
            }
    

5 个答案:

答案 0 :(得分: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个字符,但只会在单词结尾后将其剪掉(所以你不会在结尾处返回半个单词)

答案 1 :(得分:3)

我们在谈论文字,字母还是字母?

如果说:

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

的问候,

答案 2 :(得分:2)

您可以在mysql中使用substring函数

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

答案 3 :(得分:2)

试试这个:

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

结果输出:mayank

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