seran128 Posted November 14, 2006 Share Posted November 14, 2006 OK I have my codethat displays a dynamic menu likeitem1 | item2 | item3 |the problem is the last | i need to trim it offso i should get item1 | item2 | item3[code]function getmenu(){ global $connection; $sql=mysql_query("SELECT * from menu order by sort_order") or die(mysql_error()); $getmenu .= "<table width='100%' border='0'><tr><td><span class='copy'> "; while($row=mysql_fetch_array($sql)) { $getmenu .= "<a href=javascript:menulink(" . $row['ID'] . ")>" . stripslashes($row['title']) . " | "; } $getmenu .= "</span></td></table>"; return $getmenu;}[/code] Link to comment https://forums.phpfreaks.com/topic/27277-trim-last-values/ Share on other sites More sharing options...
Jenk Posted November 14, 2006 Share Posted November 14, 2006 reading the manual for trim, or rtrim may help. Especially the optional argument.. Link to comment https://forums.phpfreaks.com/topic/27277-trim-last-values/#findComment-124738 Share on other sites More sharing options...
SharkBait Posted November 14, 2006 Share Posted November 14, 2006 I dont know if there is a better way but I do it like this:[code]$links = "item1 | item2 | item3 |";substr($links, 0, strlen($links)-2);[/code]I'm sure there is an easier way but I just knock of the last few chacters if you know there will always be 1 or 2 characters after the string. Link to comment https://forums.phpfreaks.com/topic/27277-trim-last-values/#findComment-124739 Share on other sites More sharing options...
kenrbnsn Posted November 14, 2006 Share Posted November 14, 2006 You can use the [url=http://www.php.net/rtrim]rtrim()[/url] function:[code]<?php$str = 'item1 | item2 | item3 |';$str = rtrim($str,' |');?>[/code]or you could build the menu slightly differently:[code]<?phpfunction getmenu(){ global $connection; $sql=mysql_query("SELECT * from menu order by sort_order") or die(mysql_error()); $getmenu = "<table width='100%' border='0'><tr><td><span class='copy'> "; $tmp = array(); while($row=mysql_fetch_assoc($sql)) { $tmp[] = '<a href="javascript:menulink(' . $row['ID'] . ')">' . stripslashes($row['title']); } $getmenu .= " " . implode(" | ",$tmp) . " "; $getmenu .= "</span></td></table>"; return($getmenu);}?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/27277-trim-last-values/#findComment-124740 Share on other sites More sharing options...
printf Posted November 14, 2006 Share Posted November 14, 2006 one way...[code]function getmenu(){ global $connection; $sql = mysql_query ( "SELECT * from menu order by sort_order" ) or die ( mysql_error () ); $hold = array (); while ( $row = mysql_fetch_array ( $sql, MYSQL_ASSOC ) ) { $hold[] = "<a href='javascript:menulink(" . $row['ID'] . ");'>" . stripslashes ( $row['title'] ) . "</a>"; } return "<table width='100%' border='0'><tr><td><span class='copy'> " . implode ( ' | ', $hold ) . " </span></td></table>";}[/code] Link to comment https://forums.phpfreaks.com/topic/27277-trim-last-values/#findComment-124741 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.