teaser...trucate an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

teaser...trucate an array

Post 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?
Flamie
Forum Contributor
Posts: 166
Joined: Mon Mar 01, 2004 3:19 pm

Post 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.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Check out our Useful posts thread :wink:


Search for: chopping of text
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post by mesz »

Cheers guys...when I get it sorted I'll make sure I come back and post the code.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

How about:

Code: Select all

$message = implode(' ', array_slice(preg_split('/[\s]+/', $message), 0, 50));
(#10850)
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

hawleyjr wrote:Check out our Useful posts thread :wink:


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',
)
:roll:
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post 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...

Code: Select all

SELECT SUBSTR(news, 1,50)
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";
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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 :wink:
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Post 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);



?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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 :wink:
Nope - entirely possible to use MySQL (for example) to return only the first x words...

Mac
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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 :wink:
Nope - entirely possible to use MySQL (for example) to return only the first x words...

Mac
:oops: Mind showing me an example?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Jcart wrote: :oops: 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'
(#10850)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

arborint wrote:
Jcart wrote: :oops: 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
Post Reply