...

View Full Version : word limit



maria2005
06-10-2005, 10:36 AM
Hi all,

got this code, which am currently using on my input form cuts the description down to 20 characters, but what am looking for now is a string which cuts off at the end of a sentence. For example:

The Buyers/Prospects have responded to advertisements in the National Press, Magazines and via inserts. Approximately 35% of customers pay by credit or debit card with Multi-buyers being very high.

to show:

The Buyers/Prospects have responded to advertisements in the National Press, Magazines and via inserts.

<?php
$string = $row_rsLists['listdescription'];
print wordlimit($string, 20);
?>

Does anyone have any ideas?

Feel free to use the script above.

Many thanks

Maria

delinear
06-10-2005, 10:54 AM
Just the first sentence you want? Try this:

<?php
function return_first_sentence($str) {
$length = strlen($str);
for($i=0; $i < $length; $i++) {
if(substr($str, $i, 1) == '.') {
return substr($str, 0, $i+1);
}
}
return $str;
}

$string = 'The Buyers/Prospects have responded to advertisements in the National Press, Magazines and via inserts. Approximately 35% of customers pay by credit or debit card with Multi-buyers being very high.';
print return_first_sentence($string);
?>

maria2005
06-10-2005, 11:04 AM
Just the first sentence you want? Try this:

<?php
function return_first_sentence($str) {
$length = strlen($str);
for($i=0; $i < $length; $i++) {
if(substr($str, $i, 1) == '.') {
return substr($str, 0, $i+1);
}
}
return $str;
}

$string = 'The Buyers/Prospects have responded to advertisements in the National Press, Magazines and via inserts. Approximately 35% of customers pay by credit or debit card with Multi-buyers being very high.';
print return_first_sentence($string);
?>

that's a nice bit of string, however each category displays a different sentence and therefore cannot just specify one individual sentence. Is it possible to have a string which looks at the description and cuts off at the end of the first sentence?

<?php echo $row_rsPromo['listdescription']; ?> this is the insert from my database, which shows the description on the page.

hope that makes sense.

Many thanks

Maria

mattyod
06-10-2005, 11:05 AM
If it is always going to be the first sentence you want to extract then this does it very simply.


<?php
$string = 'The Buyers/Prospects have responded to advertisements in the National Press, Magazines and via inserts. Approximately 35% of customers pay by credit or debit card with Multi-buyers being very high.';

$shortString = explode (".", $string);
echo $shortString[0].".";
?>


:thumbsup:

delinear
06-10-2005, 11:18 AM
<?php echo return_first_sentence($row_rsPromo['listdescription']); ?>

maria2005
06-10-2005, 11:20 AM
you know what I've figured it out amazingly! geez it really feels good when you do it yourself!

<?php function return_first_sentence($str) { $length = strlen($str);
for($i=0; $i < $length; $i++) {
if(substr($str, $i, 1) == '.') {
return substr($str, 0, $i+1);
}
}
return $str;
}

$string = $row_rsPromo['listdescription'];
print return_first_sentence($string); ?>

something like that - however a couple of problems - want to return two sentences, so it ends at the second sentence, thinking just change

<?php function return_first_sentence($str) to
<?php function return_second_sentence($str) not to sure if works though.

also get this message:
Fatal error: Cannot redeclare return_first_sentence() (previously declared in c:\web\jpart\promo.php:164) in c:\web\jpart\promo.php on line 164

as its a repeat region

???

Maria

delinear
06-10-2005, 11:39 AM
Okay, change the function to this:


function return_first_sentence($str, $num=1) {
$length = strlen($str);
$inc=1;
for($i=0; $i < $length; $i++) {
if(substr($str, $i, 1) == '.') {
if($inc==$num) {
return substr($str, 0, $i+1);
}
$inc++;
}
}
return $str;
}

Now you can call this by either using:

<?php echo return_first_sentence($row_rsPromo['listdescription']); ?>
This will return the default one message. Alternatively you can specify exactly how many sentences you want to call using the following format:

//For two sentences:
<?php echo return_first_sentence($row_rsPromo['listdescription'], 2); ?>

//For five sentences:
<?php echo return_first_sentence($row_rsPromo['listdescription'], 5); ?>

//etc
Hope this helps :)

Harry Armadillo
06-10-2005, 11:48 AM
Could you keep it from truncating sentences like, "I just spend $4.97 on a 7.5 ounce latte." ?

delinear
06-10-2005, 11:57 AM
Could you keep it from truncating sentences like, "I just spend $4.97 on a 7.5 ounce latte." ?
Hmm, not so straightforward. Regular expressions might be better although a quick and dirty way would be to just split the chunk where it encounters a period followed by a space ". " - still not ideal but slightly better.


function return_first_sentence($str, $num=1) {
$length = strlen($str);
$inc=1;
for($i=0; $i < $length; $i++) {
if(substr($str, $i, 2) == '. ') {
if($inc==$num) {
return substr($str, 0, $i+1);
}
$inc++;
}
}
return $str;
}

I think that should do it, though I might attempt some form of regex if no-one else fancies it :o

Kid Charming
06-10-2005, 04:11 PM
It looks like you're pulling this sentence from a db. If so, you can pull a pre-truncated version through your query:



SELECT
LEFT( fieldname,LOCATE('. ',fieldname,x) ) AS truncated
FROM
table


Where x is approximately the length of the string you want returned. That will find the first sentence-ending period after x and return the string up to that point.

Of course, this assumes your sentences will all end with a period. As has been mentioned above, a regex is probably your most bulletproof option. I prefer to dodge the whole issue, and just truncate between words and add an ellipses, if possible:



SELECT
CONCAT( LEFT( fieldname,LOCATE(' ',fieldname,x) ), '...') AS truncated
FROM
table



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum