Substr Help needed please

Discussion in 'PHP' started by Basti, Aug 24, 2008.

  1. #1
    Hello everyone,

    i have a code snippet which shows random comments on my topsite

    And with substr i made it to show only 40 character and replace the rest with "...."
    Well is there a chance so i could add a link after the ....???

    Here are the codes

    the substr:
    
    $review = $TMPL['review'];
    $limit = '40';
    
       if (strlen($review) > $limit) {
          $review = substr($review, 0, strrpos(substr($review, 0, $limit), ' ')) . '...';
          }
    
    $TMPL['review'] = $review;
    
    Code (markup):
    and this is the code for the link:
    ( that works when i place it after the substr, but not in the substr itself, where i need it, because i wanna show a link to the full review only if it was shortened )
    
    $TMPL['random_review'] = "<a href=\"index.php?a=stats&amp;u={$TMPL['username']}&amp;all_reviews=1\">more</a> 
    ";
    
    Code (markup):
    I allready tried to place it different spots and variations, but all throwed errors like unexpected t_string out.

    I hope you now what i mean and hope someone can help me out, thanks
     
    Basti, Aug 24, 2008 IP
  2. rcadble

    rcadble Peon

    Messages:
    109
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Here, I wrote a function for you:
    function limitText($text, $limit, $linkurl) {
    	return (strlen($text) > $limit) == true ? substr($text, 0, $limit) . "... <a href=\"{$linkurl}\">more</a>" : $text;
    }
    Code (markup):
    $limit is the number of characters to show before showing the elipsis. $text is the text to limit the length of. $linkurl is the url to link the "more" link to.

    Example:
    echo limitText("Hello, this message will cut off after 30 characters!  Hello, this message will cut off after 30 characters! ", 30, "http://www.google.com");
    Code (markup):
     
    rcadble, Aug 24, 2008 IP