|
|
|
A lot of sites will create a text "preview" on the front page of thier site (say a news story) with
a "more" link to get the full story.
You can easily chop a string at a certain number of characters using substr():
| <?php
$charLimit = 25;
$myText = "The quick brown fox jumped over the lazy dog";
$teaserText = substr($myText,0,$charLimit) . "... <a href="<your more link>">more</a>";
echo $teaserText;
?> | |
Which echos: "The quick brown fox jumpe... more"
If you wanted to chop the text at a word break, rather than mid-word, you can do this:
| <?php
$wordLimit = 7;
$myText = "The quick brown fox jumped over the lazy dog";
$teaserText = '';
$words = explode(' ',$myText);
$i = 0;
while($i < $wordLimit)
{
$i++;
$teaserText .= $words[$i]." ";
}
$teaserText .= "... <a href="<your more link>">more</a>";
?> | |
Which echos: "The quick brown fox jumped over the... more" |
|
| Takes an array and returns a string, suitable for inputing in an SQL statement
Categories : Arrays, Strings, PHP | | | columned txt file to array()? Categories : Arrays, Strings, Regexps, PHP | | | How to ifconfig down/up a list of IP's Categories : Arrays, Strings, Filesystem, PHP | | | Grab images from one or more URLs and save them to a specified local directory. Categories : PHP, Filesystem, Strings, Arrays | | | Compare two texts and display a block of text with the differences between them. Categories : PHP, PHP Classes, Filesystem, Strings, Arrays | | | Function to create a separated list Categories : PHP, Arrays, Strings | | | Variable serialization and unserialization. Loading and saving variable structures
to and from file. Categories : Arrays, Filesystem, Variables, Strings, PHP | | | WWW interface to Unix Manual(phpMan)
Categories : Program Execution, Strings, Arrays, PHP | | | Get TemplateMonster data Categories : Arrays, Ecommerce, PHP, Strings | | | How to Get a character array from a string Categories : PHP, Strings, Arrays | | | Can the word DO be used in arrays? Categories : Arrays, PHP, Strings | | | PHP Script to find url links in a page Categories : PHP, URLs, Regexps, Arrays | | | Tag content retrieval from websites with preg_match Categories : PHP, Regexps, Arrays, HTML and PHP | | | Array values from javascript to php Categories : PHP, Java Script, Arrays | | | clearing variables in php3 Categories : Variables, Arrays, PHP | |
| | | | José Santos wrote :965
Limit text to N characters, N integer, and for more information click on the link, is very useful in websites (example: news).
This is very simple to implement.
| |
|
|
|