R0CKY Posted January 3, 2007 Share Posted January 3, 2007 I'm trying to edit an existing php page so that it trims a string to 20 characters, but I cannot figure out how to do it (n00b).The relevant loop is below..[quote]$show = "20";$template = "<span class=\"style1\"> {number}.<a href=\"{filelink}\">{filename} </a></span><br>"; $i = 1;while ($file = mysql_fetch_object($result)) { $line = str_replace("{number}", $i, $template); $line = str_replace("{filelink}", "$config[3]/pafiledb.php?action=file&id=$file->file_id", $line); $line = str_replace("{[b]filename[/b]}", $file->file_name, $line); if ($list == "newest") { $infoline = str_replace("{date}", date("n/j/y", $file->file_time), $info); } if ($list == "downloads") {$infoline = str_replace("{downloads}", $file->file_dls , $info); } if ($list == "rating") { $ntv = $file->file_totalvotes - 1; $infoline = str_replace("{rating}", @round($file->file_rating/$ntv,2), $info); $infoline = str_replace("{votes}", $ntv, $infoline); } $line = str_replace("{info}", $infoline, $line); echo $line; $i++;}[/quote]The part I need trimmed back to 20 chars is the filename, so that when the $line variable is output to the browser, the filename part is no longer than 20 chars - but I am not clued up on PHP so although I know I need to use SUBSTR, I do not know how to apply it in the above situation.Any help would be much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/32754-help-with-string-manipulation/ Share on other sites More sharing options...
obsidian Posted January 3, 2007 Share Posted January 3, 2007 Basically, once you have your filename, just apply substr() to it, and assign the result to a variable:[code]<?php$filename = "mySuperLongFilenameWithWayTooManyCharacters";$shortFile = substr($filename, 0, 20);?>[/code]That's it! Quote Link to comment https://forums.phpfreaks.com/topic/32754-help-with-string-manipulation/#findComment-152470 Share on other sites More sharing options...
R0CKY Posted January 3, 2007 Author Share Posted January 3, 2007 Hmmm, your code has $filename, but my code does not have the $, it has {filename} - will it work the same, so my new code would change from $line = str_replace([b]"{filename}"[/b], $file->file_name, $line);to $shortFile = substr($filename, 0, 20); $line = str_replace[b]($shortfile[/b], $file->file_name, $line);I have a feeling that's not right...? Quote Link to comment https://forums.phpfreaks.com/topic/32754-help-with-string-manipulation/#findComment-152497 Share on other sites More sharing options...
trq Posted January 3, 2007 Share Posted January 3, 2007 [code=php:0]$line = str_replace("{filename}", substr($file->file_name,0,20), $line);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/32754-help-with-string-manipulation/#findComment-152500 Share on other sites More sharing options...
R0CKY Posted January 3, 2007 Author Share Posted January 3, 2007 That worked Thorpe, thank you.What is actually happening with this piece of code?[b]$file->file_name[/b]Is it a new string being created, and it's value coming from a sql field?If anyone has the inclination, what I ultimately wanted to do with the output $line, was have it check the length of {filename} and if it was over 20, then trim it to 17, and add a string of three periods "...", that would be very cool.Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/32754-help-with-string-manipulation/#findComment-152545 Share on other sites More sharing options...
trq Posted January 3, 2007 Share Posted January 3, 2007 [quote]Is it a new string being created, and it's value coming from a sql field?[/quote]Pretty much.Add this to the top of your file....[code=php:0]function trimlen($string) { if (len($string) > 20) { $string = substr($string,0,17).'...'; } return $string}[/code]Then, change the last line I gave you to....[code=php:0]$line = str_replace("{filename}", trimlen($file->file_name), $line);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/32754-help-with-string-manipulation/#findComment-152550 Share on other sites More sharing options...
R0CKY Posted January 3, 2007 Author Share Posted January 3, 2007 I changed that line and added that code right under the <?php line, but something's not quite right, I am getting nil ouput now, just a blank page. :( Quote Link to comment https://forums.phpfreaks.com/topic/32754-help-with-string-manipulation/#findComment-152556 Share on other sites More sharing options...
Jessica Posted January 4, 2007 Share Posted January 4, 2007 Missing a ;[code]return $string;[/code].If your page doesn't load, it can't compile. Quote Link to comment https://forums.phpfreaks.com/topic/32754-help-with-string-manipulation/#findComment-152593 Share on other sites More sharing options...
R0CKY Posted January 4, 2007 Author Share Posted January 4, 2007 Thank you Jesi, I feel we are getting closer, but I still get no output :(Here is the full page, with the added code in bold - something of which is stopping the page from executing.[quote]<style type="text/css">A:link {text-decoration: underline; font-weight: bold; color:#CCCCCC} A:visited {text-decoration: underline; font-weight: bold;; color: #CCCCCC} A:active {text-decoration: none;} A:hover {text-decoration: underline; color:#00FF33;}body { background-color: black; font-family: Tahoma; font-size: 8pt; color: 00CC00}</style><?php[b]function trimlen($string) { if (len($string) > 20) { $string = substr($string,0,17).'...'; } return $string;}[/b]/* paFileDB 3.1 ©2001/2002 PHP Arena Written by Todd todd@phparena.net http://www.phparena.net Keep all copyright links on the script visible Please read the license included with this script for more information.*/$path = "**removed**"; //This is the full system path to your paFileDB folder. You will need to obtain this information from a phpinfo.php file, or from your host.$show = "20"; //How many files do you want to show on the list$template = "<span class=\"style1\"> {number}.<a href=\"{filelink}\">{filename} </a></span><br>"; //Template for each line.//Don't modify anything below this line.require $path. "/includes/mysql.php";$pafiledb_sql->connect($db);$config = $pafiledb_sql->query($db,"SELECT * FROM $db[prefix]_settings",1);switch ($list) { case newest: $result = $pafiledb_sql->query($db, "SELECT * FROM $db[prefix]_files ORDER BY file_time DESC LIMIT 0,$show ", 0); $info = "Added on {date}"; break; case downloads: $result = $pafiledb_sql->query($db, "SELECT * FROM $db[prefix]_files ORDER BY file_dls DESC LIMIT 0,$show ", 0); $info = "{downloads}"; break; case rating: $result = $pafiledb_sql->query($db, "SELECT * FROM $db[prefix]_files ORDER BY file_rating/(file_totalvotes - 0) DESC LIMIT 0,$show", 0); $info = "{rating}/10 - {votes} Votes"; break; default: die("You didn't select the type of list to show. Please see the paFileDB manual for more information"); break;}$i = 1;while ($file = mysql_fetch_object($result)) { $line = str_replace("{number}", $i, $template); $line = str_replace("{filelink}", "$config[3]/pafiledb.php?action=file&id=$file->file_id", $line); [b]$line = str_replace("{filename}", trimlen($file->file_name), $line);[/b] //$line = str_replace("{filename}", $file->file_name, $line); if ($list == "newest") { $infoline = str_replace("{date}", date("n/j/y", $file->file_time), $info); } if ($list == "downloads") {$infoline = str_replace("{downloads}", $file->file_dls , $info); } if ($list == "rating") { $ntv = $file->file_totalvotes - 1; $infoline = str_replace("{rating}", @round($file->file_rating/$ntv,2), $info); $infoline = str_replace("{votes}", $ntv, $infoline); } $line = str_replace("{info}", $infoline, $line); echo $line; $i++;}?>[/quote] Quote Link to comment https://forums.phpfreaks.com/topic/32754-help-with-string-manipulation/#findComment-152817 Share on other sites More sharing options...
obsidian Posted January 4, 2007 Share Posted January 4, 2007 len() is not a PHP function. You need to be using strlen() instead:[code]<?phpfunction trimlen($String, $len = 20) { if (strlen($String) > $len) { $String = substr($String, 0, ($len - 3)) . '...'; } return $String;}?>[/code]Notice, I also set an optional $len variable in your function that defaults to 20. This way, when you call the function, you can designate how many characters you wish to display. Quote Link to comment https://forums.phpfreaks.com/topic/32754-help-with-string-manipulation/#findComment-152870 Share on other sites More sharing options...
R0CKY Posted January 4, 2007 Author Share Posted January 4, 2007 That's awesome Obsidian, many thanks!WIP -> http://www.ghostrecon.net/index-mods.htmlYou can see the list working now on the top right, thank you for getting past this sticking point! Quote Link to comment https://forums.phpfreaks.com/topic/32754-help-with-string-manipulation/#findComment-152953 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.