NullPoiиteя
NullPoiиteя

Reputation: 57322

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

Upvotes: 1

Views: 6258

Answers (5)

Asraful Haque
Asraful Haque

Reputation: 1125

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

Upvotes: 0

Query Master
Query Master

Reputation: 7097

Try This:

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

Result Output: mayank

Upvotes: 2

Nick
Nick

Reputation: 6346

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 (e.g. 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)

Upvotes: 5

Teson
Teson

Reputation: 6736

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,

Upvotes: 3

Venu
Venu

Reputation: 7289

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

Upvotes: 2

Related Questions