Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder olidenia's Avatar
    Join Date
    Oct 2009
    Location
    Sitting In Front Of A Screen
    Posts
    110
    Thanks
    16
    Thanked 4 Times in 4 Posts

    Limit the characters script problem.

    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?

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

    echo 
    "$post_cond_es ..."
    Last edited by olidenia; 11-11-2010 at 07:57 PM.
    It's easy once you know how...

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Try:

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

  • Users who have thanked MattF for this post:

    olidenia (11-11-2010)

  • #3
    Regular Coder olidenia's Avatar
    Join Date
    Oct 2009
    Location
    Sitting In Front Of A Screen
    Posts
    110
    Thanks
    16
    Thanked 4 Times in 4 Posts
    Thanks, it fixed the time out but now I get this error:

    Code:
    Warning: strpos() [function.strpos]: Offset not contained in string in C:\xampp\htdocs\admin\ok\listacondiciones.php on line 18
    It's easy once you know how...

  • #4
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    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?

  • #5
    Regular Coder olidenia's Avatar
    Join Date
    Oct 2009
    Location
    Sitting In Front Of A Screen
    Posts
    110
    Thanks
    16
    Thanked 4 Times in 4 Posts
    Fixed it like this, thanks for every ones help.

    PHP Code:
    $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($text0$length);
    }

    echo 
    "$post_cond_es"
    It's easy once you know how...

  • #6
    Regular Coder olidenia's Avatar
    Join Date
    Oct 2009
    Location
    Sitting In Front Of A Screen
    Posts
    110
    Thanks
    16
    Thanked 4 Times in 4 Posts
    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:

    PHP Code:
    $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($text0$length);

    It's easy once you know how...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •