teaser...trucate an array
Moderator: General Moderators
teaser...trucate an array
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?
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?
I'd say:
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.
Code: Select all
<?
$message = "your long message here";
$words = explode($message, " ");
for($i=0;$i<50;$i++)
{
echo $words[$i]." ";
}
?>- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
How about:
Code: Select all
$message = implode(' ', array_slice(preg_split('/[\s]+/', $message), 0, 50));(#10850)
From Feyd:
viewtopic.php?t=25351#130156
feyd wrote:outputsCode: 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); ?>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', )
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
I could do that...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
Code: Select all
SELECT SUBSTR(news, 1,50)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";
?>- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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.
-
litebearer
- Forum Contributor
- Posts: 194
- Joined: Sat Mar 27, 2004 5:54 am
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);
?>- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
twigletmac wrote:Nope - entirely possible to use MySQL (for example) to return only the first x words...Jcart wrote:depends if you want words cut in half or nottwigletmac wrote:If the data's coming from a database then you could also use SQL to return the first part of the article.
Mac
Mac
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
I don't think you can do exactly that, but you could probably get close with somethink like:Jcart wrote:Mind showing me an example?
Code: Select all
'SELECT LEFT(fieldname, LOCATE(" ",fieldname,' . $n_words * $avg_word_length . ')) AS shortarticle'(#10850)
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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.arborint wrote:I don't think you can do exactly that, but you could probably get close with somethink like:Jcart wrote:Mind showing me an example?
Code: Select all
'SELECT LEFT(fieldname, LOCATE(" ",fieldname,' . $n_words * $avg_word_length . ')) AS shortarticle'
Mac