Jump to content

Breaking Long Paragraph Into Read More Link...


thara

Recommended Posts

hello.. all and happy new year 2013..

 

My problem is I am trying to get some testimonials from the database and need to echo them in a div. The DIV has created with fix width and height values. In this case I need to display every testimonials with a link of 'read more' to full one. Here in the DIV I want to limit to only 50 words from the testimonials... can any body tell me how I can do this...

 

Thank you...

Link to comment
Share on other sites

<?php
$echo = $getFig["news_content"];  //change this to the content variable you are using
if(strlen($echo) <= 30){
$bar = $echo;
}if(strlen($echo) > 30){
$bar = substr($echo, 0, 31 /* if you change this, you must change the 30s in the above to be one less then this number as shown */)."<ahref=\"--fileLink--\">Read More...</a>";
}

echo htmlspecialchars($bar);
?>

 

I used this to shorten my news feeds for one of my sites, works perfectly!

Link to comment
Share on other sites

here I am try to get a solution with counting words not in characters... I need to check how many words in the testimonials and it has below 50 words display full testimonial or it has over 50 words I need echo 50 words long testimonial with a read more link to full one..

 

I tried something like this.. it counting words properly. but I am trying print paragraph with the link.. I got a problem..

 

    $sinhalaAlphabet = 'අආඇඈඉඊඋඌඍඎඏඐඑඒඓඔඕඖකඛගඝඞඟචඡජඣඤඥඦටඨඩඪණඬතථදධනඳපඵබභමඹයරලවශෂසහළෆකංකඃක්කාකැකෑකිකීකුකූකෘකෙකේකෛකොකෝකෞකෟකෲකෳක෴    ';

   $q = "SELECT * FROM sinhala WHERE s_id=12 LIMIT 1";
   $r = mysqli_query($dbc, $q);

   $row = mysqli_fetch_array($r, MYSQL_ASSOC);
       $id = $row['s_id'];
       $sin = $row['sinhala'];
       $sin = nl2br($sin);
       $num = str_word_count($sin, 0, $sinhalaAlphabet);

       if( $num <= 50){
           $bar = $sin;
       }

   echo '<div class="testimonial">';
       echo '<p>'. $bar . '</p></div>';

Link to comment
Share on other sites

Since you are looking to use "words" rather than "characters"; look into how array_slice may help you accomplish your goal. (hint: create an array of the content using the SPACE)

 

This forum is where you try stuff, and we help you fix bugs. You have to try it on your own. You've been given a direction.

Link to comment
Share on other sites

I tried something like this..

 

    $sinhalaAlphabet = 'අආඇඈඉඊඋඌඍඎඏඐඑඒඓඔඕඖකඛගඝඞඟචඡජඣඤඥඦටඨඩඪණඬතථදධනඳපඵබභමඹයරලවශෂසහළෆකංකඃක්කාකැකෑකිකීකුකූකෘකෙකේකෛකොකෝකෞකෟකෲකෳක෴    ';

   $q = "SELECT * FROM sinhala WHERE s_id=13 LIMIT 1";
   $r = mysqli_query($dbc, $q);

   $row = mysqli_fetch_array($r, MYSQL_ASSOC);
       $id = $row['s_id'];
       $sin = $row['sinhala'];
       $sin = nl2br($sin);
       $num = str_word_count($sin, 0, $sinhalaAlphabet);

       if( $num <= 30){
           $shortened = $sin;
       }

       if( $num > 30){

           $length = 30;
           $shortened = implode(' ',array_slice(str_word_count($sin,0, $sinhalaAlphabet ),0,$length)) . '...<a href="#">read more</a>';
       }

       //echo htmlspecialchars($bar);

   echo '<div class="testimonial">';
       echo '<p>'. $shortened . '</p></div>';

 

 

But I got this error msg..

 

Warning: array_slice() expects parameter 1 to be array, integer given in C:\wamp\www\sin.php on line 125

 

Warning: implode() [function.implode]: Invalid arguments passed in C:\wamp\www\sin.php on line 125

 

line number 125 is : $shortened = implode(' ',array_slice(str_word_count($sin,0, $sinhalaAlphabet ),0,$length)) . '...<a href="#">read more</a>';

Link to comment
Share on other sites

Psuedo code...

 

$content = place some text into a variable

 

$array01 = explode(" ", $content) --- explode your variable into an array using the SPACE as the separator

 

$count = count($array01) --- count the number of elements in the array

 

if($count<=50) {

$teaser = $content --- if 50 or less then you can use the $content as your 'teaser'

}ELSE{

$array02 = array_splice($array01, 0, 50) --- if greater than 50; use array_splice to place the first 50 elements into a new array.

 

$teaser = implode(" ", $array02) --- Then use implode to create your 'teaser'; using the SPACE as the separator.

}

Link to comment
Share on other sites

Now Its working...

 

    $sinhalaAlphabet = 'අආඇඈඉඊඋඌඍඎඏඐඑඒඓඔඕඖකඛගඝඞඟචඡජඣඤඥඦටඨඩඪණඬතථදධනඳපඵබභමඹයරලවශෂසහළෆකංකඃක්කාකැකෑකිකීකුකූකෘකෙකේකෛකොකෝකෞකෟකෲකෳක෴    ';

   $q = "SELECT * FROM sinhala WHERE s_id=12 LIMIT 1";
   $r = mysqli_query($dbc, $q);

   $row = mysqli_fetch_array($r, MYSQL_ASSOC);
       $id = $row['s_id'];
       $sin = $row['sinhala'];
       $sin = nl2br($sin);
       $num = str_word_count($sin, 0, $sinhalaAlphabet);
       print_r($num);

       if( $num <= 50){
           $shortened = $sin;
       }

       if( $num > 50){

           $short = explode(" ", $sin);
           $length = 50;
           $shortened = implode(' ',array_slice( $short,0,$length)) . '...<a href="#">read more</a>';
       }

       //echo htmlspecialchars($bar);

   echo '<div class="testimonial">';
       echo '<p>'. $shortened . '</p></div>';

 

can I know is there any other ways to do this than I have done so far?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.