aksdesigns Posted December 12, 2009 Share Posted December 12, 2009 Hey guys, I need to truncate some text so it always stays on one line. I need it to truncate after a certain amount of characters or width. So, if the text is long, I want it truncated but if it's not long - I want it left alone. Unfortunately, I can't break out of an echo to write an if statement as I want to truncate text pulled for a certain row. I have some code at the moment that doesn't work very well. Here it is: echo "</td><td class='listingtitle'>Artist:</td><td><a href='http://megalyrics.net/search.html?c=".$row[artist]."' title='View ".$row[artist]." Lyrics'>".$row[artist]."</a></td></tr><td class='listingtitle'>Album:</td><td><a href='http://megalyrics.net/search.html?c=".$row[album]."' title='View ".$row[album]." Lyrics'>".preg_replace('/\s+?(\S+)?$/', '', substr($row[album], 0, 30))."...</a></td></tr>"; It returns this result (on the right hand table showing the newest items - the album part): http://www.megalyrics.net/index.html I'm a PHP novice, could anyone offer an suggestions? Thanks! Link to comment https://forums.phpfreaks.com/topic/184909-if-else-statement-inside-echo-needed/ Share on other sites More sharing options...
ngreenwood6 Posted December 12, 2009 Share Posted December 12, 2009 looks like it is already there you just need to change the substr() 30 to a lesser value. Link to comment https://forums.phpfreaks.com/topic/184909-if-else-statement-inside-echo-needed/#findComment-976136 Share on other sites More sharing options...
aksdesigns Posted December 12, 2009 Author Share Posted December 12, 2009 I do have something there but it truncates whatever is there and seems to be working oddly (smaller album names are truncated really small and longer ones are truncating how i want them to) I need something there to tell it to not touch ones that don't need truncating. Link to comment https://forums.phpfreaks.com/topic/184909-if-else-statement-inside-echo-needed/#findComment-976143 Share on other sites More sharing options...
aksdesigns Posted December 12, 2009 Author Share Posted December 12, 2009 Sorry to bump but does anyone have any ideas on this? Link to comment https://forums.phpfreaks.com/topic/184909-if-else-statement-inside-echo-needed/#findComment-976202 Share on other sites More sharing options...
benwilhelm Posted December 12, 2009 Share Posted December 12, 2009 You might try something like this, though I haven't tested it: // Check length of $row['album'] and create var $albumName based on it if (strlen($row['album']) >= 30) { $albumName = substr($row['album'],0,30) . '...' ; } else { $albumName = $row['album'] ; } // Echo out table using previously created $albumName echo "</td><td class='listingtitle'>Artist:</td><td><a href='http://megalyrics.net/search.html?c=".$row[artist]."' title='View ".$row[artist]." Lyrics'>".$row[artist]."</a></td></tr><td class='listingtitle'>Album:</td><td><a href='http://megalyrics.net/search.html?c=".$row[album]."' title='View ".$row[album]." Lyrics'>".preg_replace('/\s+?(\S+)?$/', '', $albumName."...</a></td></tr>"; Link to comment https://forums.phpfreaks.com/topic/184909-if-else-statement-inside-echo-needed/#findComment-976214 Share on other sites More sharing options...
aksdesigns Posted December 12, 2009 Author Share Posted December 12, 2009 I get an error saying: Parse error: syntax error, unexpected ';' in /home/highr7/public_html/megalyrics/index.html on line 75 which is the last bit of this (what i used of your code)... // Check length of $row['album'] and create var $albumName based on it if (strlen($row['album']) >= 30) { $albumName = substr($row['album'],0,30) . '...' ; } else { $albumName = $row['album'] ; } // Echo out table using previously created $albumName echo "</td><td class='listingtitle'>Artist:</td><td><a href='http://megalyrics.net/search.html?c=".$row[artist]."' title='View ".$row[artist]." Lyrics'>".$row[artist]."</a></td></tr><td class='listingtitle'>Album:</td><td><a href='http://megalyrics.net/search.html?c=".$row[album]."' title='View ".$row[album]." Lyrics'>".preg_replace('/\s+?(\S+)?$/', '', $albumName."...</a></td></tr>"; I've attached the file if that helps (without the code you sent and as php because it's usually html but won't allow me to upload that)... [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/184909-if-else-statement-inside-echo-needed/#findComment-976225 Share on other sites More sharing options...
ngreenwood6 Posted December 12, 2009 Share Posted December 12, 2009 There is a syntax error, the closing parenthesis for preg_replace is missing. Link to comment https://forums.phpfreaks.com/topic/184909-if-else-statement-inside-echo-needed/#findComment-976228 Share on other sites More sharing options...
aksdesigns Posted December 12, 2009 Author Share Posted December 12, 2009 but it's inside the echo with a full stop either side of it? Link to comment https://forums.phpfreaks.com/topic/184909-if-else-statement-inside-echo-needed/#findComment-976231 Share on other sites More sharing options...
ngreenwood6 Posted December 12, 2009 Share Posted December 12, 2009 try this: // Check length of $row['album'] and create var $albumName based on it if (strlen($row['album']) >= 30) { $albumName = substr($row['album'],0,30) . '...' ; } else { $albumName = $row['album'] ; } // Echo out table using previously created $albumName echo "</td><td class='listingtitle'>Artist:</td><td><a href='http://megalyrics.net/search.html?c=".$row[artist]."' title='View ".$row[artist]." Lyrics'>".$row[artist]."</a></td></tr><td class='listingtitle'>Album:</td><td><a href='http://megalyrics.net/search.html?c=".$row[album]."' title='View ".$row[album]." Lyrics'>".preg_replace('/\s+?(\S+)?$/', '', $albumName)."...</a></td></tr>"; all i did was add a ) after $albumName because you werent closing the preg_replace function Link to comment https://forums.phpfreaks.com/topic/184909-if-else-statement-inside-echo-needed/#findComment-976234 Share on other sites More sharing options...
aksdesigns Posted December 12, 2009 Author Share Posted December 12, 2009 It now doesn't error but even when I change a value to 200 or more, it still looks the same as it first did. Here's the code: // Check length of $row['album'] and create var $albumName based on it if (strlen($row['album']) >= 200) { $albumName = substr($row['album'],0,30) . '...' ; } else { $albumName = $row['album'] ; } // Echo out table using previously created $albumName echo "</td><td class='listingtitle'>Artist:</td><td><a href='http://megalyrics.net/search.html?c=".$row[artist]."' title='View ".$row[artist]." Lyrics'>".$row[artist]."</a></td></tr><td class='listingtitle'>Album:</td><td><a href='http://megalyrics.net/search.html?c=".$row[album]."' title='View ".$row[album]." Lyrics'>".preg_replace('/\s+?(\S+)?$/', '', $albumName)."...</a></td></tr>"; And the link showing that it still shows the same: http://www.megalyrics.net Link to comment https://forums.phpfreaks.com/topic/184909-if-else-statement-inside-echo-needed/#findComment-976237 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.