简体   繁体   中英

how to get first some line from a paragraph

I have a paragraph stored in database i want to get only first five line from . it how to do this ?

should i convert first array to srting ? if yes then how to do this ?

if its string than i can do this by

$str='mayank kumar swami mayank kumar swami';
$var= strlen($str);
for($i=0;$i<8;$i++){
    echo $str[$i];
}
  1. or how to get only 200 word from the database by sql ?

i know it can be done by css easily shows in Show first line of a paragraph nut i want to do this by php or sql query

what i doing

$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>";
            }
        }

There are a number of solutions to this problem.

If you want to split it by number of words, something similar to what user247245 posted:

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

My preferred method however is getting all the full words up until a certain point (eg 200 characters):

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

The above will chop the string at 200 characters, however will only chop it after the end of a word (so you won't get half a word returned at the end)

Are we talking words, lines or letters?

If words:

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

regards,

You can use substring function in mysql

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

returns

Quadr

I suggest you do with sql as it reduces the amount of data transfer between you db server and application server.

So, Now you modify to this

$article_result = mysql_query("SELECT article_name, authore, SUBSTRING(content,1,200) as content FROM article ORDER BY time DESC LIMIT 1",$connection);

Try This:

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

Result Output: mayank

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM