thara Posted January 2, 2013 Share Posted January 2, 2013 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... Quote Link to comment https://forums.phpfreaks.com/topic/272600-breaking-long-paragraph-into-read-more-link/ Share on other sites More sharing options...
Pikachu2000 Posted January 2, 2013 Share Posted January 2, 2013 Look at MySQL's SUBSTRING_INDEX() function for that. Quote Link to comment https://forums.phpfreaks.com/topic/272600-breaking-long-paragraph-into-read-more-link/#findComment-1402701 Share on other sites More sharing options...
White_Lily Posted January 2, 2013 Share Posted January 2, 2013 <?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! Quote Link to comment https://forums.phpfreaks.com/topic/272600-breaking-long-paragraph-into-read-more-link/#findComment-1402709 Share on other sites More sharing options...
thara Posted January 2, 2013 Author Share Posted January 2, 2013 white_Lily your coding is working... English characters but when I am using other languages as string there is a problem. String length become differ. how can we fix this problem? Quote Link to comment https://forums.phpfreaks.com/topic/272600-breaking-long-paragraph-into-read-more-link/#findComment-1402732 Share on other sites More sharing options...
litebearer Posted January 2, 2013 Share Posted January 2, 2013 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) Quote Link to comment https://forums.phpfreaks.com/topic/272600-breaking-long-paragraph-into-read-more-link/#findComment-1402737 Share on other sites More sharing options...
thara Posted January 2, 2013 Author Share Posted January 2, 2013 thanks for response.. can you elaborate your answer with an example? Thank you.. Quote Link to comment https://forums.phpfreaks.com/topic/272600-breaking-long-paragraph-into-read-more-link/#findComment-1402741 Share on other sites More sharing options...
thara Posted January 2, 2013 Author Share Posted January 2, 2013 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>'; Quote Link to comment https://forums.phpfreaks.com/topic/272600-breaking-long-paragraph-into-read-more-link/#findComment-1402778 Share on other sites More sharing options...
Jessica Posted January 2, 2013 Share Posted January 2, 2013 What is the problem you got? Quote Link to comment https://forums.phpfreaks.com/topic/272600-breaking-long-paragraph-into-read-more-link/#findComment-1402782 Share on other sites More sharing options...
thara Posted January 2, 2013 Author Share Posted January 2, 2013 My problem is when I am trying to convert long testimonials to the 50 words length paragraph with a link of read more... So can you tell me how can I print 50 words length paragraph with a read more link... Quote Link to comment https://forums.phpfreaks.com/topic/272600-breaking-long-paragraph-into-read-more-link/#findComment-1402786 Share on other sites More sharing options...
Jessica Posted January 2, 2013 Share Posted January 2, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/272600-breaking-long-paragraph-into-read-more-link/#findComment-1402788 Share on other sites More sharing options...
thara Posted January 2, 2013 Author Share Posted January 2, 2013 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>'; Quote Link to comment https://forums.phpfreaks.com/topic/272600-breaking-long-paragraph-into-read-more-link/#findComment-1402807 Share on other sites More sharing options...
litebearer Posted January 2, 2013 Share Posted January 2, 2013 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. } Quote Link to comment https://forums.phpfreaks.com/topic/272600-breaking-long-paragraph-into-read-more-link/#findComment-1402808 Share on other sites More sharing options...
Jessica Posted January 2, 2013 Share Posted January 2, 2013 Did you look up array_slice? The error is very clear about what's wrong. Quote Link to comment https://forums.phpfreaks.com/topic/272600-breaking-long-paragraph-into-read-more-link/#findComment-1402809 Share on other sites More sharing options...
thara Posted January 2, 2013 Author Share Posted January 2, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/272600-breaking-long-paragraph-into-read-more-link/#findComment-1402814 Share on other sites More sharing options...
Jessica Posted January 2, 2013 Share Posted January 2, 2013 can I know is there any other ways to do this than I have done so far? Probably. Quote Link to comment https://forums.phpfreaks.com/topic/272600-breaking-long-paragraph-into-read-more-link/#findComment-1402820 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.