...

View Full Version : Resolved Limit the characters script problem.



olidenia
11-11-2010, 07:00 PM
Im having problems with this script, the function is to limit the characters to 50 without cutting a word, so it gets the string from sql, strips the tags and then limits to the lenght indicated, the problem is that if there are fewer characters than indicated in the script ($length =50), my browser and server goes in to a 60second time out.

Otherwise the script works perfect.

Any help?


$length =50;
$text = strip_tags($row[cond_es]) ;
$check = substr($text,$length,1);
if($check != " "){
while ( $check != " "){
$length = $length + 1 ;
$check = substr($text,$length,1);
}}
$post_cond_es = substr($text, 0, $length);

echo "$post_cond_es ...";

MattF
11-11-2010, 07:21 PM
Try:



$length = 50;
$text = strip_tags($row[cond_es]) ;
$length = strpos($text, ' ', ($length-1));
$post_cond_es = substr($text, 0, $length);

olidenia
11-11-2010, 07:28 PM
Thanks, it fixed the time out but now I get this error:


Warning: strpos() [function.strpos]: Offset not contained in string in C:\xampp\htdocs\admin\ok\listacondiciones.php on line 18

MattF
11-11-2010, 07:33 PM
Post your code again, as it is now. Also, echo $length just before the substr and see if it's set. Which line is line 18, btw?

olidenia
11-11-2010, 07:55 PM
Fixed it like this, thanks for every ones help.



$length = 50; //Max length of characters to display.

if (strlen(strip_tags($row[cond_es] ))< $length) //If characters are les than Max length, than just display the characters.
{
$post_cond_es = strip_tags($row[cond_es]); //Else process them and limit.
}
else
{
$text = strip_tags($row[cond_es]) ;
$length = strpos($text, ' ', ($length-1));
$post_cond_es = substr($text, 0, $length);
}

echo "$post_cond_es";

olidenia
11-11-2010, 08:05 PM
I have done a mod to the script, I have had to give it some margin, I had errors when there where exactly 50 chars so:


$length = 50;
if (strlen(strip_tags($row[cond_es] ))< $length+5)
{
$post_cond_es = strip_tags($row[cond_es]);
}
else
{
$text = strip_tags($row[cond_es]) ;
$length = strpos($text, ' ', ($length-1));
$post_cond_es = substr($text, 0, $length);
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum