Page 1 of 1
teaser...trucate an array
Posted: Sat May 27, 2006 2:15 pm
by mesz
OK, I'll admit, I haven't got a scooby-doo how to begin this so I haven't got any code for people to impove on.
That said...
What function allows an echo statement to be truncated after a certain amount of words?
For instance I have a form, I display the name of the author and the title of the article and then I want the next textarea to be truncated after 50 words and insert a link to the remainder of the article.
What function do I need?
Posted: Sat May 27, 2006 2:18 pm
by Flamie
I'd say:
Code: Select all
<?
$message = "your long message here";
$words = explode($message, " ");
for($i=0;$i<50;$i++)
{
echo $words[$i]." ";
}
?>
The downside is that its assuming all words are separated by spaces, so I dunno if you have like new lines in there or something it might cause some trouble.
Posted: Sat May 27, 2006 2:27 pm
by hawleyjr
Check out our
Useful posts thread
Search for: chopping of text
Posted: Sat May 27, 2006 2:29 pm
by mesz
Cheers guys...when I get it sorted I'll make sure I come back and post the code.
Posted: Sat May 27, 2006 2:39 pm
by Christopher
How about:
Code: Select all
$message = implode(' ', array_slice(preg_split('/[\s]+/', $message), 0, 50));
Posted: Sat May 27, 2006 2:44 pm
by hawleyjr
hawleyjr wrote:Check out our
Useful posts thread
Search for: chopping of text
From Feyd:
viewtopic.php?t=25351#130156
feyd wrote:Code: Select all
<?php
$text = 'Jack went to the store to get himself a large bottle of whiskey';
preg_match('#^\s*(.{26,}?)\s+.*$#', $text, $match);
var_export($match);
?>
outputs
Code: Select all
array (
0 => 'Jack went to the store to get himself a large bottle of whiskey',
1 => 'Jack went to the store to get',
)

Posted: Sat May 27, 2006 3:07 pm
by twigletmac
If the data's coming from a database then you could also use SQL to return the first part of the article.
Mac
Posted: Sat May 27, 2006 3:25 pm
by mesz
twigletmac wrote:If the data's coming from a database then you could also use SQL to return the first part of the article.
Mac
I could do that...
but I think php would do a much better job of checking for not splitting words and so on
OK, going down the php route I got this mash of code but the only thing that displays is the title...any idea why?
Code: Select all
<?php
include("admin/config.php");
$db = mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($dbname) or die("Cannot connect to database");
$query = "select * from thenews order by id $format LIMIT $limit";
$result = mysql_query($query);
echo "<table>";
$match=array();
while ($display = mysql_fetch_array($display)) {
$paragraph = preg_match('#^\s*(.{26,}?)\s+.*$#', $display[news], $match);
echo "
<tr><td><span style="color: $hcolor; font-weight: bold;">$display[title]</span></td></tr>
<tr><td><span style="color: $bcolor">$match[0]</span></td></tr>\n";
if ($info == 1) {
echo "<tr><td><div>by $display[author]-$display[date]</div></td></tr>\n";
}
}
"</td></tr>
</table>\n";
?>
Posted: Sat May 27, 2006 4:31 pm
by John Cartwright
twigletmac wrote:If the data's coming from a database then you could also use SQL to return the first part of the article.
Mac
depends if you want words cut in half or not

Posted: Sat May 27, 2006 5:27 pm
by RobertGonzalez
Check out the second and third user comment on
the substr() page of the PHP Manual. There are two really nice functions for returning words without breaking them.
Posted: Sat May 27, 2006 5:50 pm
by litebearer
hmmm
Code: Select all
<?PHP
#########################
# This little function will take a string,
# truncate it to a specific length,
# make sure it is not truncated in
# the middle of a word and add
# three trailing periods.
#########################
Function display_teaser($article,$display_length) {
if(strlen($article)>$display_length) {
$display_portion = substr($article,0,$display_length);
} else {
$display_portion = $article;
}
$true=0;
while ($true < 1) {
if(substr($display_portion, -1) != " ") {
$display_portion = substr($display_portion, 0, -1);
}else{
$true = 1;
}
}
$display_portion = $display_portion . "...";
return $display_portion;
}
################
# this is just an example
# of its usuage
#################
$test_article = "The first articles about The Big Lie were not published in French newspapers. When the book began to appear in French bookstores and Thierry Meyssan had not been invited yet to any television program, two newspapers, one in Chile and another in Hungary, talked about his research on 9/11 with interest. Later, French dailies Le Monde and Libération wrote full pages to accuse him after the author’s appearance at the Thierry Ardisson’s show on March 16. The position of the two newspapers that accused him of -lying - and - revisionism - was completely accepted by the French media as a whole. But abroad, countless newspapers highlighted the pertinence of the research.
<P>
In Argentina, Page 12 explained that Thierry Meyssan rightfully questioned the official version whose contradictions and silences are numerous. In Switzerland, Le Courrier described The Big Lie as a book written in a clear and documented way in which the final result has an absolute coherence and the fabrication is convincingly revealed In China and Russia , the research awoke great interest. In the Balkans, the book was particularly welcomed and its first translation was made into Slovene. On the other hand, the book was published divided into 35 episodes in two Yugoslavian newspapers: Polítika and Draganic.";
##################
# change the value to
# whatever you desire
##################
$test_len = 100;
echo display_teaser($test_article,$test_len);
?>
Posted: Sun May 28, 2006 6:37 am
by twigletmac
Jcart wrote:twigletmac wrote:If the data's coming from a database then you could also use SQL to return the first part of the article.
Mac
depends if you want words cut in half or not

Nope - entirely possible to use MySQL (for example) to return only the first x words...
Mac
Posted: Sun May 28, 2006 11:57 am
by John Cartwright
twigletmac wrote:Jcart wrote:twigletmac wrote:If the data's coming from a database then you could also use SQL to return the first part of the article.
Mac
depends if you want words cut in half or not

Nope - entirely possible to use MySQL (for example) to return only the first x words...
Mac

Mind showing me an example?
Posted: Sun May 28, 2006 1:08 pm
by Christopher
Jcart wrote: 
Mind showing me an example?
I don't think you can do exactly that, but you could probably get close with somethink like:
Code: Select all
'SELECT LEFT(fieldname, LOCATE(" ",fieldname,' . $n_words * $avg_word_length . ')) AS shortarticle'
Posted: Sun May 28, 2006 3:00 pm
by twigletmac
arborint wrote:Jcart wrote: 
Mind showing me an example?
I don't think you can do exactly that, but you could probably get close with somethink like:
Code: Select all
'SELECT LEFT(fieldname, LOCATE(" ",fieldname,' . $n_words * $avg_word_length . ')) AS shortarticle'
You can use
SUBSTRING_INDEX() to gather the first x words based on words being separated by spaces. It just saves returning an entire article from the database when you only want the first little bit. IIRC we may do a bit of tidying on the PHP side as well but the FTP server's down so I can't get at the script to check unfortunately.
Mac