Help with string output limit

Discussion in 'PHP' started by EHamilton, Nov 12, 2006.

  1. #1
    What's up? I'm pretty noobish at php and need some help with implementing this code correctly without causing errors. I have a line that reads...

    echo " <p class=\"desc\">".$srow['discription']."</p>\n";

    I need it to basically read this...

    echo "
    <p class=\"desc\">
    <?
    function truncateString($str, $max, $rep = '...') {
    if(strlen($str) > $max) {
    $leave = $max - strlen($rep);
    return substr_replace($str, $rep, $leave);
    } else {
    return $str;
    }
    }

    print truncateString(".$srow['discription'].", 25);
    ?>
    </p>\n";

    What I'm trying to do is set the string to read a certain amount of characters. I know I'm getting an error probably because I'm putting php inside of echo lmfao. Any help is appreciated :eek:
     
    EHamilton, Nov 12, 2006 IP
  2. maiahost

    maiahost Guest

    Messages:
    664
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #2
    First off you'll get an error on the first echo : here is a bit of modification which worked fine:
    
    <?php
    function truncateString($str, $max, $rep = '...') {
    if(strlen($str) > $max) {
    $leave = $max - strlen($rep);
    return substr_replace($str, $rep, $leave);
    } else {
    return $str;
    }}
    $s1='saddddddadasdsadasdadsadasdasdasdadasdsadsasd';
    $string = truncateString($s1, 25);
    echo $string;
    ?>
    
    Code (markup):
    if you want to echo something put it inside the <? ?>
     
    maiahost, Nov 12, 2006 IP
  3. EHamilton

    EHamilton Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the help. I got it to work but I have a list of 3 videos and the first time I only got 1 to show up, I played around with the code a bit and got 2 to show up and the rest of the page was blank. I PMed you the complete code, maybe you can help. Thanks again!
     
    EHamilton, Nov 13, 2006 IP