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
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 <? ?>
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!