NealeWeb Posted May 19, 2011 Share Posted May 19, 2011 I am wanting to display large text blocks from a database but i would like to stop the text if its longer than 600 characters and display Read more... as a link. Can someone please help me? Quote Link to comment https://forums.phpfreaks.com/topic/236850-read-more-if-text-more-than-600-characters/ Share on other sites More sharing options...
anupamsaha Posted May 19, 2011 Share Posted May 19, 2011 The theory would be like this: <?php $t = "Your text goes here Your text goes here Your text goes here Your text goes here Your text goes here Your text goes here Your text goes here Your text goes here Your text goes here"; echo substr($t,0,10), (strlen($t) > 10) ? ' <a href="yourlink">more...</a>' : ''; // taken 10 as limit. you can use 600 here or make it a variable. ?> Quote Link to comment https://forums.phpfreaks.com/topic/236850-read-more-if-text-more-than-600-characters/#findComment-1217478 Share on other sites More sharing options...
NealeWeb Posted May 19, 2011 Author Share Posted May 19, 2011 Wow that is perfect it works great thankyou so much Quote Link to comment https://forums.phpfreaks.com/topic/236850-read-more-if-text-more-than-600-characters/#findComment-1217481 Share on other sites More sharing options...
anupamsaha Posted May 19, 2011 Share Posted May 19, 2011 Please mark the topic as "resolved" if you are satisfied with the help. Quote Link to comment https://forums.phpfreaks.com/topic/236850-read-more-if-text-more-than-600-characters/#findComment-1217497 Share on other sites More sharing options...
NealeWeb Posted May 19, 2011 Author Share Posted May 19, 2011 Ok sorry onne more thing i am trying to use this in an existing echo but im having trouble. I get this error: Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' on line 48 This is what i have tried: echo '<div align="center"> <table border="0" cellpadding="10" cellspacing="0" width="969" background="imgs/reviewbox.png" height="157"> <tr> <td height="19" ><span class="font1">' . $jtitle . '</span></td> <td height="19" > <p align="right"><a href="review.php?review_id=' . $revid . '"><span class="font">View Full review</span></a></td> </tr> <tr> Line 48 >>> <td valign="top" colspan="2"><span class="font">substr($t,0,, (strlen($t) > ? ' <a href="yourlink">Read more...</a>' : '';</span></td> <<< Line 48 </tr> </table> </div>'; Is it possible to make it a variable like this in any way: <?php $t = "Your text"; $y= substr($t,0,, (strlen($t) > ? ' <a href="yourlink">Read more...</a>' : ''; echo $y; ?> I am very new at php so im sure im making some pretty silly mistakes. Quote Link to comment https://forums.phpfreaks.com/topic/236850-read-more-if-text-more-than-600-characters/#findComment-1217526 Share on other sites More sharing options...
anupamsaha Posted May 19, 2011 Share Posted May 19, 2011 No worries. Here you go: echo '<div align="center"> <table border="0" cellpadding="10" cellspacing="0" width="969" background="imgs/reviewbox.png" height="157"> <tr> <td height="19" ><span class="font1">' . $jtitle . '</span></td> <td height="19" > <p align="right"><a href="review.php?review_id=' . $revid . '"><span class="font">View Full review</span></a></td> </tr> <tr> <td valign="top" colspan="2"><span class="font">' . substr($t,0, . ((strlen($t) > ? ' <a href="yourlink">Read more...</a>' : '') . '</span></td> </tr> </table> </div>'; If you are trying to take a variable, do this: <?php $t = "Your text"; $y = substr($t,0, . ((strlen($t) > ? ' <a href="yourlink">Read more...</a>' : ''); // see that I have enclosed the conditional statement in braces. echo $y; ?> Quote Link to comment https://forums.phpfreaks.com/topic/236850-read-more-if-text-more-than-600-characters/#findComment-1217533 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.