By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,216 Members | 1,001 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,216 IT Pros & Developers. It's quick & easy.

Shortening text before full display

P: 5
Hi Guys,

Sorry if these are noob questions but i have a few issues with a website im trying to build.

Ok its a review site, where the user can type their review into a submit page.. got all that working, what i need to know is, is there a way to display say the first 50 words of an article followed by a read more and how do you make a multi page article (although these are possibly the same thing) Just need the names of the commands or something??

Also I have had to put a meta redirect on my main page into a folder, for some reason code that works in /news/ or /login/ etc will not work on the home page... its got me baffled as it was a copy of one of the other pages with only a few alterations to re-pointing the includes...

Thanks in advance
Feb 14 '08 #1
Share this Question
Share on Google+
5 Replies


ronverdonk
Expert 2.5K+
P: 4,258
Something like this?
[php]
<?php
$text="Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.";
if (isset($_GET['text']) AND $_GET['text'] == 'all')
echo $text;
else {
echo substr($text,0,50);
echo '<br><a href="'.$PHP_SELF.'?text=all" >read more .....</a>';
}
?>[/php]
Ronald
Feb 14 '08 #2

Markus
Expert 5K+
P: 6,050
Something like this?
[php]
<?php
$text="Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.";
if (isset($_GET['text']) AND $_GET['text'] == 'all')
echo $text;
else {
echo substr($text,0,50);
echo '<br><a href="'.$PHP_SELF.'?text=all" >read more .....</a>';
}
?>[/php]
Ronald
If you dont want to words chopping up, you can use a regexp to check:
[php]
$_text="Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.";
if(!$_GET['text'] == "all")
{
preg_match('#^\s*(.{21,}?)\s+.*$#s', $_text, $match);
echo $match[1];
}
else
{
echo $_text;
}
[/php]
heres where i got this from.
Feb 15 '08 #3

P: 5
Something like this?
[php]
<?php
$text="Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.";
if (isset($_GET['text']) AND $_GET['text'] == 'all')
echo $text;
else {
echo substr($text,0,50);
echo '<br><a href="'.$PHP_SELF.'?text=all" >read more .....</a>';
}
?>[/php]
Ronald
Ronald thats exactly what I mean, i need to implement it so its pulling $text from the database, this is my current code:

[PHP]<?php $result=mysql_query("SELECT * FROM article WHERE cat='hardware' ORDER BY time DESC LIMIT 5");
while ($row=mysql_fetch_array($result))

{
echo "<br /><div class='text'><span style='float: left;'><b>{$row["title"]}</b></span><span style='float: right;'><b>Posted By {$row["poster"]}</b><br />{$row["time"]}</span><br /><br />";
print "<p>{$row["body"]}<br /></p><span style='float: left;'><b>Category: <a href='{$row["cat"]}.php'>{$row["cat"]}</a></b></span><span style='float: right;'><b>Source: <a href='{$row["link"]}'>{$row["link"]}</a></b></span><br /></div><br />";
} ?>[/PHP]

Can you help?
Feb 15 '08 #4

P: 5
Hey guys,

I got it working... using a mixture of both suggestions... I'd tell you that ur the best but im guessing thats why you hang around here :)
Feb 15 '08 #5

ronverdonk
Expert 2.5K+
P: 4,258
That's right! Pleasure to help you out. See you next time.

Ronald
Feb 15 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.