[font=Verdana, Arial, Helvetica][size=2][color=midnightblue]I’m really new to php and mysql so please forgive my ignorance. The only way I can do this stuff right now is with Dreamweaver MX and recordsets.
I have a recordset that displays an article summary from my database and I only want to display the first few words followed by …
This is what I have now and it retreives the whole summary.
[/color][/size][/font]
<?php echo $row_featured['summary']; ?>
I figured out how to trim the output to a number of characters using substr like below, but it cuts off words.
Can anyone please help me change this code so it display the first 10 or 12 words followed by … ? If it’s easier to do it with mysql query I’m certainly open to that as well.
Thanks for the quick reply. I have looked at that, but I’m afraid I just can’t figure out how to incorporate the fuctions into my existing code.
The example below seems the most appropriate, but can you please give me some advice for turning this:
<?php echo $row_featured['summary']; ?>
into this:
Truncates a string at a certain position without �cutting� words into senseless pieces.
I hope this is usefull for some of you and that it is not another redundant note (I didn't find one) ...
<?php
$string = "Truncates a string at a certain position without »cutting« words into senseless pieces.";
$maxlength = 75;
$extension = " ...";
function truncate_string ($string, $maxlength, $extension) {
// Set the replacement for the "string break" in the wordwrap function
$cutmarker = "**cut_here**";
// Checking if the given string is longer than $maxlength
if (strlen($string) > $maxlength) {
// Using wordwrap() to set the cutmarker
// NOTE: wordwrap (PHP 4 >= 4.0.2, PHP 5)
$string = wordwrap($string, $maxlength, $cutmarker);
// Exploding the string at the cutmarker, set by wordwrap()
$string = explode($cutmarker, $string);
// Adding $extension to the first value of the array $string, returned by explode()
$string = $string[0] . $extension;
}
// returning $string
return $string;
}
// This Will output:
// "Truncates a string at a certain position without �cutting� ..."
echo truncate_string ($string, $maxlength, $extension);
?>